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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11225051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:03:22  来源:igfitidea点击:

How to read XML file in c#?

c#xmllinqlinq-to-xml

提问by Hemant Kothiyal

I have following XML file, i want to know best way to read this XML file

我有以下 XML 文件,我想知道阅读此 XML 文件的最佳方法

<MyFile> 
  <Companies> 
    <Company>123</Company> 
    <Company>456</Company>
    <Company>789</Company> 
  </Companies> 
</MyFile>

As an output i need collection of values like "123,456,789" or it could be array of string[]

作为输出,我需要收集诸如“123,456,789”之类的值,或者它可以是字符串数组[]

Can we use Linq to xml? How?

我们可以使用Linq to xml吗?如何?

回答by spender

var xmlStr=@"<MyFile> 
  <Companies> 
    <Company>123</Company> 
    <Company>456</Company>
    <Company>789</Company> 
  </Companies> 
</MyFile>";

var xDoc = XDocument.Parse(xmlStr);
var companyIds = xDoc.Descendants("Company").Select(e => (int)e);

回答by Daren Thomas

var xdoc = XDocument.Load(PATH_TO_FILE);
var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray();

This will give you a string[].

这会给你一个string[].

回答by Habib

Use LINQ to XML, Include using System.Xml.Linq;

使用 LINQ to XML,包括 using System.Xml.Linq;

   XDocument xmlDoc = XDocument.Load("yourfile.xml");
   var test = xmlDoc.Descendants("Companies").Elements("Company").Select(r => r.Value).ToArray();
   string result = string.Join(",", test);

Output would be:

输出将是:

123,456,789

123,456,789

回答by Habib

In dataset you can read xml file

在数据集中,您可以读取 xml 文件

Following are lines of code to read XML file in DataSet

以下是在 DataSet 中读取 XML 文件的代码行

DataSet dsMenu = new DataSet(); //Create Dataset Object

dsMenu.ReadXml("XMLFILENAME.XML"); // Read XML file in Dataset

DataTable dtXMLFILE// Create DatyaTable object

dtXMLFILE= dsMenu.Tables[0]; // Store XML Data in Data Table 

回答by Keplah

In the past, I have used an XmlReaderand had no difficulties.

过去,我使用过XmlReader,并且没有任何困难。

MSDN Documentation: http://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx

MSDN 文档:http: //msdn.microsoft.com/en-us/library/system.xml.xmlreader(v= vs.110).aspx

It is very straightforward and the documentation is pretty well written. A quick demonstration of how to use it:

它非常简单,文档也写得很好。如何使用它的快速演示:

XmlReader reader = XmlReader.Create(targetFile);

while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
            if (reader.Name.Equals("Company")
            {
                // Read the XML Node's attributes and add to string
            }
            break;
    }
}

回答by thezar

string pathToXmlFile = @"C:\test.xml";
XElement patternDoc = XElement.Load(pathToXmlFile);
List<string> values = new List<string>();
foreach (var element in patternDoc.Elements("Companies").Elements("Company"))
{
   values.Add(element.Value);
}