HandlerFactory.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. namespace WeiXin.Library.Handlers
  7. {
  8. /// <summary>
  9. /// 处理器工厂类
  10. /// </summary>
  11. public class HandlerFactory
  12. {
  13. /// <summary>
  14. /// 创建处理器
  15. /// </summary>
  16. /// <param name="requestXml">请求的xml</param>
  17. /// <returns>IHandler对象</returns>
  18. public static IHandler CreateHandler(string requestXml)
  19. {
  20. IHandler handler = null;
  21. if (!string.IsNullOrEmpty(requestXml))
  22. {
  23. //解析数据
  24. XmlDocument doc = new System.Xml.XmlDocument();
  25. doc.LoadXml(requestXml);
  26. XmlNode node = doc.SelectSingleNode("/xml/MsgType");
  27. if (node != null)
  28. {
  29. XmlCDataSection section = node.FirstChild as XmlCDataSection;
  30. if (section != null)
  31. {
  32. string msgType = section.Value;
  33. switch (msgType)
  34. {
  35. case "text":
  36. handler = new TextHandler(requestXml);
  37. break;
  38. case "event":
  39. handler = new EventHandler(requestXml);
  40. break;
  41. }
  42. }
  43. }
  44. }
  45. return handler;
  46. }
  47. }
  48. }