Using XPath in C# for get all the node values from XML

梦想与她 提交于 2019-12-23 23:14:01

问题


I have got XML below:

<tcm:Component ID="tcm:481-636667" IsEditable="false" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xmlns:xlink="http://www.w3.org/1999/xlink">
  <tcm:Context>
    <tcm:Publication xlink:type="simple" xlink:title="07 Internal Test Publication" xlink:href="tcm:0-481-1"/>
    <tcm:OrganizationalItem xlink:type="simple" xlink:title="System Resources" xlink:href="tcm:481-92640-2"/>
  </tcm:Context> 
  <tcm:Data>
    <tcm:Title>IBE - Skywards</tcm:Title>
    <tcm:Type>Normal</tcm:Type>
    <tcm:Schema xlink:type="simple" xlink:title="Resources" xlink:href="tcm:481-190471-8"/>
    <tcm:Content>
      <Resources xmlns="http://www.sdltridion.com/tridion/schemas">
        <Text>
          <Key>SKYRL_MBD</Key>
          <Value>Miles Breakdown</Value>
        </Text>
        <Text>
          <Key>ltSR_MB.Text</Key>
          <Value>View Miles Breakdown</Value>
        </Text>
        <Text>
          <Key>ltSR_HMB.Text</Key>
          <Value>Hide Miles Breakdown</Value>
        </Text>
        <Text>
          <Key>SKYRL_MBD_LK</Key>
          <Value>Miles Breakdown</Value>
        </Text>    
      </Resources>
    </tcm:Content>
    <tcm:Metadata>
      <Metadata xmlns="http://www.sdltridion.com/tridion/schemas">
        <Language>
          <Language>English</Language>
        </Language>
      </Metadata>
    </tcm:Metadata>  
  </tcm:Data>
</tcm:Component>

Now I want to write a method in C# which will take this XML as input and will return all the "Key" and "Value" data in List.

Please suggest.


回答1:


First, declare the lists to keep values:

using System.Collections.Generic;

List<string> keysList = new List<string>();
List<string> valuesList = new List<string>();

Then:

using System.Xml; // System.Xml.dll

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml); // Load(file)

var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
ns.AddNamespace("xlink", "http://www.w3.org/1999/xlink");

foreach (XmlNode node in doc.SelectNodes("//*[local-name()=\"Key\"]"))
{
    keysList.Add(node.InnerText);
}
foreach (XmlNode node in doc.SelectNodes("//*[local-name()=\"Value\"]"))
{
    valuesList.Add(node.InnerText);
}   

if you don't need XML DOM, only XPath to evaluate:

using System.Xml.XPath; // System.Xml.dll

XPathDocument doc = null;
using (TextReader reader = new StringReader(xml))
{
    doc = new XPathDocument(reader); // specify just path to file if you have such one
}
XPathNavigator nav = doc.CreateNavigator();
foreach (XPathNavigator node in (XPathNodeIterator)nav.Evaluate("//*[local-name()=\"Key\"]"))
{
     keysList.Add(node.Value);
}
foreach (XPathNavigator node in (XPathNodeIterator)nav.Evaluate("//*[local-name()=\"Value\"]"))
{
     valuesList.Add(node.Value);
}



回答2:


Use XElement or XDocument ( Linq2Xml )

XElement xml = XElement.Parse("inputxml");
var keys = xml.Descendants("Key");

OP says he uses .Net 2.0. Then this won't work!




回答3:


Have a look at the classes in the System.Xml.Linq namespace. If you start with an XDocument you can load your XML and just query the contents using Linq.



来源:https://stackoverflow.com/questions/5946255/using-xpath-in-c-sharp-for-get-all-the-node-values-from-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!