zoukankan      html  css  js  c++  java
  • 两种读取微信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
    
  • 相关阅读:
    stylus 安装
    Vue中img的src属性绑定与static文件夹
    vue-cli的build的文件夹下没有dev-server.js文件,怎么配置mock数据
    easyui中的datebox空间,起始时间不受限制,终止时间不能选择起始时间选中的时间靠前的时间
    python实现文件下载图片视频
    python爬虫 urllib库基本使用
    python面向对象的三大特征
    常见网页状态码
    说说为什么会有ssl.CertificateError报错
    python-魔术方法
  • 原文地址:https://www.cnblogs.com/chunyih/p/3859458.html
Copyright © 2011-2022 走看看