如何使用Xpath读取xml子点头 [英] how to read xml child nod using Xpath
本文介绍了如何使用Xpath读取xml子点头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何使用Xpath读取以下xml版本的innertext(即"1.0.0.0")
Please tell me how to read version innertext(i.e "1.0.0.0") using Xpath for the below xml
<package>
<workflowprocesses>
<workflowprocess>
</workflowprocess>
<workflowprocess>
<activites>
<activity>
<version>1.0.0.0</version>
</activity>
</activites>
</workflowprocess>
</workflowprocesses>
</package>
推荐答案
在此处上有一些出色的XPath教程[ ^ ],它将向您显示几乎所有您需要了解的内容.
我认为类似这样的方法可能会帮您解决问题;
There are some excellent XPath tutorials over here[^] that''ll show you just about everything you need to know.
I think that something like this might do the trick for you;
using System;
using System.IO;
using System.Xml.XPath;
namespace Sandbox
{
class Program
{
static void Main(string[] args)
{
string xml = "<package><workflowprocesses><workflowprocess></workflowprocess><workflowprocess><activites><activity><version>1.0.0.0</version></activity></activites></workflowprocess></workflowprocesses></package>";
XPathDocument document = new XPathDocument(new StringReader(xml));
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator versionList = navigator.Select("/package/workflowprocesses/workflowprocess/activites/activity/version");
foreach (XPathNavigator versionElement in versionList)
{
string version = versionElement.Value;
Console.WriteLine("Version=[{0}]", version);
}
}
}
}
希望这会有所帮助,
Fredrik
Hope this helps,
Fredrik
我不知道您的xml文件中有多少个子节点"workflowprocess",但是...如果您想在该xml文件中获得"1.0.0.0",请点击此处是您的XPath:
/package/workflowprocesses/workflowprocess [last()]/activites/activity/version
i don''t know how many child node "workflowprocess" in your xml file, but... if you want to get "1.0.0.0" in that xml file, here is your XPath:
/package/workflowprocesses/workflowprocess[last()]/activites/activity/version
这篇关于如何使用Xpath读取xml子点头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文