[英]Using XPath in C# for get all the node values from XML
I have got XML below: 我有以下XML:
<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. 现在我想在C#中编写一个方法,它将此XML作为输入,并将返回List中的所有“Key”和“Value”数据。
Please suggest. 请建议。
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: 如果您不需要XML DOM,则只需要XPath来评估:
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);
}
Use XElement or XDocument ( Linq2Xml ) 使用XElement或XDocument(Linq2Xml)
XElement xml = XElement.Parse("inputxml");
var keys = xml.Descendants("Key");
OP says he uses .Net 2.0. OP说他使用.Net 2.0。 Then this won't work! 那么这不行!
Have a look at the classes in the System.Xml.Linq
namespace. 查看System.Xml.Linq
命名空间中的类。 If you start with an XDocument
you can load your XML and just query the contents using Linq. 如果您从XDocument
开始,您可以加载XML并使用Linq查询内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.