c#获取XML数据的最简单方法

时间:2021-08-25 16:28:56

I have been doing a lot of reading on LINQ to XML but unfortunately this topic (which is fairly new to me) simply will not click. Having said that, please feel free to correct any misspeaks regarding proper XML vocabulary. My goal is to take XML data, (shown below), and read it node by node. In this instance, I want to be able to open the Delimiters node, in order to obtain the values of the "one", "two", and "three" elements. Next, I would like to obtain the values of the "one", "two", and "three" elements from within the Sources/SourceType nodes.

我已经在LINQ上做了很多关于XML的阅读,但不幸的是,这个主题(对我来说是全新的)根本不需要点击。说到这里,请随时纠正关于正确的XML词汇表的任何误解。我的目标是获取XML数据(如下所示),并逐个节点地读取它。在本例中,我希望能够打开分隔符节点,以获取“1”、“2”和“3”元素的值。接下来,我想从源/SourceType节点中获取“1”、“2”和“3”元素的值。

<?xml version="1.0"?>
<Values>

  <Delimiters>
    <one>delim 1</one>
    <two>delim 2</two>
    <three>delim 3</three>
  </Delimiters>

  <Sources>

    <SourceType>
      <one>type 1</one>
      <two>type 2</two>
      <three>type 3</three>
    </SourceType>

  </Sources>

</Values>

I have read on XMLTextReader as well as XMLReader but I would like to hear from all of you what the best practices are for my situation here.

我已经阅读了XMLTextReader和XMLReader,但我想从你们所有人那里听到关于我这里的情况的最佳实践。

Thank you for reading,

感谢您的阅读,

Evan

埃文

3 个解决方案

#1


5  

You will probably want to use Linq to XML for this - parsing is straightforward:

您可能希望使用Linq to XML实现这一点——解析非常简单:

XDocument doc = XDocument.Load("test.xml");
foreach (var delimiter in doc.Descendants("Delimiters").Elements())
    Console.WriteLine(string.Format("{0} : {1}", delimiter.Name, delimiter.Value));

foreach (var type in doc.Descendants("SourceType").Elements())
    Console.WriteLine(string.Format("{0} : {1}", type.Name, type.Value));

The big advantage of Linq to XML is that not only is it very easy to query for the nodes you want (not much difference for you example but it saves a lot in more complicated XML) but the querying syntax is ubiquitous once you become familiar with Linq in general - you don't have to change the way you think.

Linq to XML的很大的优势是,它不仅是很容易查询你想要的节点(两者对你并没有明显的例子,但可以节省很多更复杂的XML)但查询语法是无处不在的,一旦你熟悉Linq一般规定——你不需要改变你的思维方式。

#2


2  

I tend to use an XmlDocument object and search the nodes using XPath expressions.

我倾向于使用XmlDocument对象并使用XPath表达式搜索节点。

// Load the xml into the reader
XmlReader reader;

XmlDocument dom = new XmlDocument()
dom.Load(reader);

XmlNodeList delimitorNode = dom.SelectSingleNode("/Values/Delimitors")
if (delmitorNode != null) {
    foreach(XmlNode childNode in delimitorNode.ChildNodes) {
        string delimitor = childNode.InnerText;
    }
}

XmlNodeList sourceNode = dom.SelectSingleNode("/Values/Sources/SourceType")
if (sourceNode != null) {
    foreach(XmlNode childNode in sourceNode.ChildNodes) {
        string sourceType = childNode.InnerText;
    }
}

W3Schools has a quick reference for XPath syntax and there are numerous guides out there for more advanced features. http://www.w3schools.com/xpath/xpath_syntax.asp

W3Schools对XPath语法有一个快速的参考,并且有大量的指南提供更高级的特性。http://www.w3schools.com/xpath/xpath_syntax.asp

#3


0  

XmlDocument is probably the easiest method to accomplish this in my opinion (you can find a lot of documentation about this). If your objects are stored in the XML file you might want to look at XML serialization and deserialization (you can pretty much read an entire XML file in one line and populate your structures).

在我看来,XmlDocument可能是实现这一点的最简单的方法(您可以找到很多关于它的文档)。如果对象存储在XML文件中,您可能希望查看XML序列化和反序列化(几乎可以在一行中读取整个XML文件并填充结构)。

#1


5  

You will probably want to use Linq to XML for this - parsing is straightforward:

您可能希望使用Linq to XML实现这一点——解析非常简单:

XDocument doc = XDocument.Load("test.xml");
foreach (var delimiter in doc.Descendants("Delimiters").Elements())
    Console.WriteLine(string.Format("{0} : {1}", delimiter.Name, delimiter.Value));

foreach (var type in doc.Descendants("SourceType").Elements())
    Console.WriteLine(string.Format("{0} : {1}", type.Name, type.Value));

The big advantage of Linq to XML is that not only is it very easy to query for the nodes you want (not much difference for you example but it saves a lot in more complicated XML) but the querying syntax is ubiquitous once you become familiar with Linq in general - you don't have to change the way you think.

Linq to XML的很大的优势是,它不仅是很容易查询你想要的节点(两者对你并没有明显的例子,但可以节省很多更复杂的XML)但查询语法是无处不在的,一旦你熟悉Linq一般规定——你不需要改变你的思维方式。

#2


2  

I tend to use an XmlDocument object and search the nodes using XPath expressions.

我倾向于使用XmlDocument对象并使用XPath表达式搜索节点。

// Load the xml into the reader
XmlReader reader;

XmlDocument dom = new XmlDocument()
dom.Load(reader);

XmlNodeList delimitorNode = dom.SelectSingleNode("/Values/Delimitors")
if (delmitorNode != null) {
    foreach(XmlNode childNode in delimitorNode.ChildNodes) {
        string delimitor = childNode.InnerText;
    }
}

XmlNodeList sourceNode = dom.SelectSingleNode("/Values/Sources/SourceType")
if (sourceNode != null) {
    foreach(XmlNode childNode in sourceNode.ChildNodes) {
        string sourceType = childNode.InnerText;
    }
}

W3Schools has a quick reference for XPath syntax and there are numerous guides out there for more advanced features. http://www.w3schools.com/xpath/xpath_syntax.asp

W3Schools对XPath语法有一个快速的参考,并且有大量的指南提供更高级的特性。http://www.w3schools.com/xpath/xpath_syntax.asp

#3


0  

XmlDocument is probably the easiest method to accomplish this in my opinion (you can find a lot of documentation about this). If your objects are stored in the XML file you might want to look at XML serialization and deserialization (you can pretty much read an entire XML file in one line and populate your structures).

在我看来,XmlDocument可能是实现这一点的最简单的方法(您可以找到很多关于它的文档)。如果对象存储在XML文件中,您可能希望查看XML序列化和反序列化(几乎可以在一行中读取整个XML文件并填充结构)。