简体   繁体   English

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

[英]C# The simplest way to obtain XML Data

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. 我一直在阅读有关LINQ to XML的大量文章,但是不幸的是,这个主题(对我而言是相当新的)根本不会点击。 Having said that, please feel free to correct any misspeaks regarding proper XML vocabulary. 话虽如此,请随时纠正与正确的XML词汇有关的所有错误提示。 My goal is to take XML data, (shown below), and read it node by node. 我的目标是获取XML数据(如下所示),并逐个节点读取它。 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. 在这种情况下,我希望能够打开Delimiters节点,以获取“ one ”,“ Two ”和“ Three ”元素的值。 Next, I would like to obtain the values of the " one ", " two ", and " three " elements from within the Sources/SourceType nodes. 接下来,我想从Sources / SourceType节点中获取“ one ”,“ 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. 我已经阅读了XMLTextReaderXMLReader但是我想听听大家对我这里情况的最佳做法。

Thank you for reading, 感谢您的阅读,

Evan 埃文

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通常来说-您不必改变思维方式。

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. W3Schools提供了有关XPath语法的快速参考,并且有许多指南提供了更多高级功能。 http://www.w3schools.com/xpath/xpath_syntax.asp http://www.w3schools.com/xpath/xpath_syntax.asp

XmlDocument is probably the easiest method to accomplish this in my opinion (you can find a lot of documentation about this). 在我看来,XmlDocument可能是完成此操作的最简单方法(您可以找到很多关于此的文档)。 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). 如果对象存储在XML文件中,则可能需要查看XML序列化和反序列化(您几乎可以在一行中读取整个XML文件并填充结构)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM