开发者

C# The simplest way to obtain XML Data

开发者 https://www.devze.com 2023-03-22 17:39 出处:网络
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

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 val开发者_运维百科ues of the "one", "two", and "three" elements from within the Sources/SourceType nodes.

<?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.

Thank you for reading,

Evan


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

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.


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

// 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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消