两种读取微信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

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

时间: 2024-10-15 19:22:15

两种读取微信xml消息的方式比较的相关文章

Android ListView两种长按弹出菜单方式

* 知识点1:ListView item:两种长按弹出菜单方式* 知识点2:ListView SimpleAdapter的使用*  知识点 3:在java代码中创建一个ListView*/ -----------------------------------------------------Activity[mw_shl_code=java,true]package org.gxl.com; public class ListOnLongClickActivity extends Activ

Hibernate实现有两种配置,xml配置与注释配置

hibernate实现有两种配置,xml配置与注释配置. (1):xml配置:hibernate.cfg.xml (放到src目录下)和实体配置类:xxx.hbm.xml(与实体为同一目录中) <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "ht

Ext_两种处理服务器端返回值的方式

1.Form表单提交返回值处理 //提交基本信息表单  f.form.submit({      clientValidation:true,      //表单提交后台处理地址      url:'/globe_background/Commonality/AccountClub.ashx?action=updateuserinfoform&uid=' + jsonDate.uid,      //数据提交方式      method:'POST',                      

XML的两种读取方法

1.xml文件内容如下: <?xml version="1.0" encoding="utf-8" ?><info> <book id ="b1" lang ="en"> <name>c++</name> <price>555</price> </book> <book id ="b2" lang =&qu

多线程基础:两种常见的创建线程的方式

一 通过继承Thread 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package thread; public class ThreadDemo1 {     public static void main(String[] args) {         Demo1 demo1 = new Demo1("zifangsky");         Demo1 demo2 = new Demo1(

ios的UIImage的两种不同的图片加载方式 tom猫

在ios的UI交互设计时,对图片的处理是难免的:不同的处理方式会对内存有不同的影响: ************************************************************ a:图片格式及NSBundle加载全路径: 1>xcode或者说苹果官方是极力推荐使用的图片格式是png 2>所有如果项目中用得是png的图片,则不用写后缀名 3>其他格式要求后缀名,特别是用UIImage加载图片时 NSBundle加载全路径的常用代码: ? 1 2 3 4 //

两种设计模式和XML解析

两种设计模式 1.单例模式 模式的保证步骤:单例(是说在一个类中只能有一个对象)三条件 1.1类构造设置私有  private  Play() { } 1.2 定义一个私有的静态的  类类型  变量 private static Play play; 1.3 定义共有的 静态的 并且 返回值类型为 类类型 的方法 public static Play GetInstance() { if(play==null) { play=new Play(); } retuen play; } 2.简单工厂

C# 用Serializer.ToXml()方法转换成两种格式的XML

常见XML格式两种: 这种是属性的格式,实体的Model属性上面加上这个特性 [XmlAttribute] <AAA aa="11" bb="22"/> 这种是标签的格式,实体的Model属性上面加上这个特性[XmlElement(ElementName = "显示的名字")] <AAA> <aa>11<aa/> <bb>22<bb/> <AAA/> 原来在VS中

XML两种读取和写入方式的例子:dom4j和jdom

一.dom4j 读写改操作,先导入jar包-1  例子1: 1 //dom4j读取 2 SAXReader reader = new SAXReader(); 3 //文档对象 4 Document document = reader.read(new File("F:\\yujun\\javaweb\\第一章\\student.xml")); 5 //根节点对象 6 Element root = document.getRootElement(); 7 //取得根节点的所有子节点集合