有用的XML函数可以在不循环的情况下提高解析性能?C#.NET

NLV*_*NLV 1 .net c# xml performance

我正在开发一个WCF服务应用程序,它对XML文件进行了大量的操作.在我开始循环节点,元素,属性和所有其他东西之前,我想知道XmlNode,XmlElement,XmlDocument和所有其他Xml相关类上可用的有用函数,它们可以用来代替循环.

例如,您可以使用CopyTo(string[]).将List转换为数组.

谢谢.

NLV

Rob*_*Rob 5

可能立即回答LINQ.使用它的"便笺簿"示例是:

using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml;

namespace LinqSample1
{
    class Program
    {
        static void Main(string[] args)
        {
            var xml = @"
        <items>
            <item>
                <name>Item 1</name>
                <price>1.00</price>
                <quantity>3</quantity>
            </item>
            <item>
                <name>Item 2</name>
                <price>1.50</price>
                <quantity>1</quantity>
            </item>
        </items>";

            var document = new XmlDocument();
            document.LoadXml(xml);

            var items = from XmlNode item in document.SelectNodes("/items/item")
                        select new
                        {
                            Name = item.SelectSingleNode("name").InnerText,
                            Price = item.SelectSingleNode("price").InnerText,
                            Quantity = item.SelectSingleNode("quantity").InnerText
                        };

            foreach (var item in items)
            {
                Console.WriteLine("Item Name: {0} costs {1} and we have a quantity of {2}", item.Name, item.Price, item.Quantity);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

性能

关于性能问题:只有你能回答这个问题,因为它非常依赖于你正在做什么以及需要它多快做到这一点.如果您的批处理过程每月运行一次并且需要30分钟才能运行,那么您可能会认为它足够快.如果代码清晰,简洁和可维护,那么重写它以便在一半的时间内运行但更复杂的不会对你,或者任何其他必须在未来维护它的人有所帮助.