两种读取微信xml消息的方式比较

直接贴代码和结果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

namespace ConsoleApplication_xml
{
    [XmlRoot("xml")]
    public class WeChatMessage
    {
        [XmlElement("ToUserName")]
        public string ToUserName { get; set; }

        [XmlElement("FromUserName")]
        public string FromUserName { get; set; }

        [XmlElement("CreateTime")]
        public int CreateTime { get; set; }

        [XmlElement("MsgType")]
        public string MsgType { get; set; }

        [XmlElement("Content")]
        public string Content { get; set; }

        [XmlElement("MsgId")]
        public long? MsgId { get; set; }
    }

    public class Program
    {
        public static T FromXml<T>(Stream stream)
        {
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stream);
        }
        
        static void Main(string[] args)
        {
            string xmlData = @"<xml>
                                 <ToUserName><![CDATA[toUser]]></ToUserName>
                                 <FromUserName><![CDATA[fromUser]]></FromUserName> 
                                 <CreateTime>1348831860</CreateTime>
                                 <MsgType><![CDATA[text]]></MsgType>
                                 <Content><![CDATA[this is a test]]></Content>
                                 <MsgId>1234567890123456</MsgId>
                               </xml>
                              ";

            // first way
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlData);

            string fromUser = doc.GetElementsByTagName("FromUserName")[0].InnerText; 
            string toUser = doc.GetElementsByTagName("ToUserName")[0].InnerText;
            string msgType = doc.GetElementsByTagName("MsgType")[0].InnerText;
            string content = doc.GetElementsByTagName("Content")[0].InnerText;
            string createTime = doc.GetElementsByTagName("CreateTime")[0].InnerText;
            string msgId = doc.GetElementsByTagName("MsgId")[0].InnerText;

            watch1.Stop();

            Console.WriteLine("first way, time = {0}", watch1.Elapsed);
            Console.WriteLine("fromUser = {0}, toUser = {1}, msgType = {2}, content = {3}, createTime = {4}, msgId = {5}",
                               fromUser, toUser, msgType, content, createTime, msgId);

            Console.WriteLine("======");

            // second way
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();

            byte[] byteArray = Encoding.UTF8.GetBytes(xmlData);
            MemoryStream stream = new MemoryStream(byteArray);

            Stopwatch watch3 = new Stopwatch();
            watch3.Start();

            WeChatMessage message = FromXml<WeChatMessage>(stream);

            watch3.Stop();
            watch2.Stop();

            Console.WriteLine("second way, total time = {0}", watch2.Elapsed);
            Console.WriteLine("second way, stream time = {0}", watch3.Elapsed);
            Console.WriteLine("fromUser = {0}, toUser = {1}, msgType = {2}, content = {3}, createTime = {4}, msgId = {5}",
                        message.FromUserName, message.ToUserName, message.MsgType, message.Content, message.CreateTime, message.MsgId);

            Console.ReadLine();
        }
    }
}

 结果:

first way, time = 00:00:00.0005323
fromUser = fromUser, toUser = toUser, msgType = text, content = this is a test,
createTime = 1348831860, msgId = 1234567890123456
======
second way, total time = 00:00:00.1302524
second way, stream time = 00:00:00.1302366
fromUser = fromUser, toUser = toUser, msgType = text, content = this is a test,
createTime = 1348831860, msgId = 1234567890123456
posted @ 2014-07-21 23:29  chunyih  阅读(2279)  评论(0编辑  收藏  举报