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用于xml吗?怎么样?
6 个解决方案
#1
11
var xdoc = XDocument.Load(PATH_TO_FILE);
var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray();
This will give you a string[]
.
这会给你一个字符串[]。
#2
6
Use LINQ to XML, Include using System.Xml.Linq;
使用LINQ to XML,包括使用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
#3
4
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
#4
3
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);
#5
3
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);
}
#6
2
In the past, I have used an XmlReader
and 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;
}
}
#1
11
var xdoc = XDocument.Load(PATH_TO_FILE);
var companies = xdoc.Descendants("Company").Select(c => (string)c).ToArray();
This will give you a string[]
.
这会给你一个字符串[]。
#2
6
Use LINQ to XML, Include using System.Xml.Linq;
使用LINQ to XML,包括使用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
#3
4
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
#4
3
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);
#5
3
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);
}
#6
2
In the past, I have used an XmlReader
and 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;
}
}