欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

XmlDocument vs XElement

程序员文章站 2022-04-10 13:52:58
``` var xmlstr = @" some_appid 1413192605 component_verify_ticket some_verify_ticket "; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i ......
  var xmlstr = @"<xml>
<appid>some_appid</appid>
<createtime>1413192605</createtime>
<infotype>component_verify_ticket</infotype>
<componentverifyticket>some_verify_ticket</componentverifyticket>
</xml>";


            stopwatch sw = stopwatch.startnew();
            for (int i = 0; i < 1000; i++)
            {
                xmldocument doc = new xmldocument();
                doc.xmlresolver = null;
                xmlnode root;
                doc.loadxml(xmlstr);
                root = doc.firstchild;
                var sencryptmsg = root["componentverifyticket"].innertext;
            }
            sw.stop();
            var docts = sw.elapsedmilliseconds;

            sw.restart();
            for (int i = 0; i < 1000; i++)
            {
                xelement xe = xelement.parse(xmlstr);
                var sencryptmsg = xe.element("componentverifyticket").value;
            }
            sw.stop();
            messagebox.show(docts + "," +sw.elapsedmilliseconds.tostring());

xmldocumentd的耗时大概是xelement 的4倍以上