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

C# 在不循环的情况下提高解析性能的有用XML函数?C.net,c#,.net,xml,performance,C#,.net,Xml,Performance,我正在开发一个WCF服务应用程序,它对XML文件进行大量操作。在开始循环节点、元素、属性和所有其他内容之前,我想知道XmlNode、XmlElement、XmlDocument和所有其他可用于“代替”循环的Xml相关类上的有用函数 例如,您可以使用CopyTostring[]将列表转换为数组 多谢各位 NLV最直接的答案可能是LINQ。使用它的便笺簿示例如下: using System; using System.Linq; using System.Xml.Linq; using System

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

例如,您可以使用CopyTostring[]将列表转换为数组

多谢各位


NLV

最直接的答案可能是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);
            }
        }
    }
}
演出


关于绩效问题:只有你能回答这个问题,因为这在很大程度上取决于你在做什么,以及你需要多快去做。如果您有一个每月运行一次的批处理流程,并且需要30分钟才能运行,那么您很可能会认为这足够快。如果代码清晰、简洁且可维护,那么重新编写代码,使其在一半的时间内运行,但更加复杂,对您或将来必须维护它的任何人都没有好处。

可能最直接的答案是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);
            }
        }
    }
}
演出


关于绩效问题:只有你能回答这个问题,因为这在很大程度上取决于你在做什么,以及你需要多快去做。如果您有一个每月运行一次的批处理流程,并且需要30分钟才能运行,那么您很可能会认为这足够快。如果代码清晰、简洁且可维护,那么重新编写代码,使其在一半的时间内运行,但更加复杂,不会对您或将来必须维护它的任何人有任何好处。

作为上述内容的一个小小扩展,建议使用较新的XDocument XElement,XAttribute etc类和Linq转换为XML,而不是更老更笨重的XmlXXX类。LINQtoXML非常适合查询,如果您愿意,可以使用XPath,它非常接近文档创建的基本要求。NLV,。。。你确定?WCF服务应用程序直到.NET3.0才推出。我刚刚验证了这一点,尝试在VS2k5中创建一个新的项目windows Framework下拉列表设置为.NET Framework 2.0,但它没有作为选项列出..哦,对不起。我使用的是VS2005,我已经在2006年11月安装了WCF模板CTP。好的,我在做3.0。但是VS2005在Linq合成方面很糟糕。顺便问一下,在VS2005中如何获得“选择框架”下拉列表。我认为它只在VS 2008和VS 2010中可用作为上述内容的一个小小扩展,建议使用较新的XDocument、XElement、XAttribute等类和Linq to XML,而不是较旧、较笨重的XmlXXX类。LINQtoXML非常适合查询,如果您愿意,可以使用XPath,它非常接近文档创建的基本要求。NLV,。。。你确定?WCF服务应用程序直到.NET3.0才推出。我刚刚验证了这一点,尝试在VS2k5中创建一个新的项目windows Framework下拉列表设置为.NET Framework 2.0,但它没有作为选项列出..哦,对不起。我使用的是VS2005,我已经在2006年11月安装了WCF模板CTP。好的,我在做3.0。但是VS2005在Linq合成方面很糟糕。顺便问一下,在VS2005中如何获得“选择框架”下拉列表。我认为它只在VS 2008和VS 2010中可用