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


当前回答

Linq到XML。

同时,VB。NET通过编译器提供了比c#更好的xml解析支持。如果你有这个选择和愿望,那就去看看吧。

其他回答

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"]是否为空,因为如果属性不存在,它将为空。

你可以:

使用XmlSerializer类 使用XmlDocument类

示例在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。

public void ReadXmlFile()
{
    string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
    XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                break;
            case XmlNodeType.Text:
                columnNames.Add(reader.Value);
                break;
            case XmlNodeType.EndElement:
                break;
        }
    }
}

可以避免使用第一个语句,只在XmlTextReader的构造函数中指定路径名。

如果您想从XML文件中检索特定的值

 XmlDocument _LocalInfo_Xml = new XmlDocument();
            _LocalInfo_Xml.Load(fileName);
            XmlElement _XmlElement;
            _XmlElement = _LocalInfo_Xml.GetElementsByTagName("UserId")[0] as XmlElement;
            string Value = _XmlElement.InnerText;