码迷,mamicode.com
首页 > 微信 > 详细

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

时间:2014-07-22 00:01:37      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:des   blog   os   io   art   cti   

直接贴代码和结果。

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

两种读取微信xml消息的方式比较,布布扣,bubuko.com

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

标签:des   blog   os   io   art   cti   

原文地址:http://www.cnblogs.com/chunyih/p/3859458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!