如何在c#中读取和解析XML文件?


当前回答

有很多方法,一些:

XmlSerializer。使用带有目标模式的类 如果您想读取,请使用XmlSerializer 将Xml中的数据加载到 类的实例。 linq2xml XmlTextReader。 XmlDocument XPathDocument(只读访问)

其他回答

下面是另一种使用Cinchoo ETL的方法,它是一个开源库,可以用很少的代码行解析xml文件。

using (var r = ChoXmlReader<Item>.LoadText(xml)
       .WithXPath("//item")
      )
{
    foreach (var rec in r)
        rec.Print();
}

public class Item
{
    public string Name { get; set; }
    public string ProtectionLevel { get; set; }
    public string Description { get; set; }
}

样本提琴:https://dotnetfiddle.net/otYq5j

免责声明:我是这个库的作者。

例如,检查XmlTextReader类。

XmlDocument从字符串或文件中读取XML。

using System.Xml;

XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml");

or

doc.LoadXml("<xml>something</xml>");

然后在它下面找到一个节点,就像这样

XmlNode node = doc.DocumentElement.SelectSingleNode("/book/title");

or

foreach(XmlNode node in doc.DocumentElement.ChildNodes){
   string text = node.InnerText; //or loop through its children as well
}

然后像这样读取节点内的文本

string text = node.InnerText;

或者读取属性

string attr = node.Attributes["theattributename"]?.InnerText

总是检查Attributes["something"]是否为空,因为如果属性不存在,它将为空。

LINQ to XML示例:

// Loading from a file, you can also load from a stream
var xml = XDocument.Load(@"C:\contacts.xml");


// Query the data and write out a subset of contacts
var query = from c in xml.Root.Descendants("contact")
            where (int)c.Attribute("id") < 4
            select c.Element("firstName").Value + " " +
                   c.Element("lastName").Value;


foreach (string name in query)
{
    Console.WriteLine("Contact's Full Name: {0}", name);
}

参考:LINQ到XML在MSDN

There are different ways, depending on where you want to get. XmlDocument is lighter than XDocument, but if you wish to verify minimalistically that a string contains XML, then regular expression is possibly the fastest and lightest choice you can make. For example, I have implemented Smoke Tests with SpecFlow for my API and I wish to test if one of the results in any valid XML - then I would use a regular expression. But if I need to extract values from this XML, then I would parse it with XDocument to do it faster and with less code. Or I would use XmlDocument if I have to work with a big XML (and sometimes I work with XML's that are around 1M lines, even more); then I could even read it line by line. Why? Try opening more than 800MB in private bytes in Visual Studio; even on production you should not have objects bigger than 2GB. You can with a twerk, but you should not. If you would have to parse a document, which contains A LOT of lines, then this documents would probably be CSV.

我写这条评论,是因为我看到了大量使用XDocument的示例。XDocument不适用于大型文档,或者当您只想验证内容是否为XML有效时。如果希望检查XML本身是否有意义,那么需要Schema。

我对建议的答案也投了反对票,因为我相信它本身就需要上述信息。假设我需要验证200M的XML是否有效,每小时10次。XDocument会浪费大量的资源。

prasanna venkatesh还指出,您可以尝试将字符串填充到数据集,它也将指示有效的XML。