ASP.NET 2.0:如何使用XPath?
问题描述:
如果我们可以在ASP.NET 2.0中的XPath通过获得的x个节点集合?然后按照他们的意见进行检查。ASP.NET 2.0:如何使用XPath?
<x-list>
<x id="1" enable="On" url="http://abc.123.dev"/>
<x id="2" enable="Off" url="http://asd.com"/>
<x id="3" enable="On" url="http://plm.xcv.tw"/>
</x-list>
感谢您的任何帮助。 瑞奇
答
下面是会检索所有的“X”节点的样本已启用:
XmlNodeList nodes = root.SelectNodes("/x-list/x[@enable='On']");
foreach (XmlNode node in nodes)
{
...
}
我发现W3Schools的一个很好的地方去寻找XPath tutorials。
答
XmlDocument xDocument = new XmlDocument();
xDocument.LoadXml(xmlString);
XmlNodeList xList = xDocument.SelectNodes("x-list/x");
if (xList.Count > 0)
{
foreach (XmlNode x in xList)
{
string id = x.Attributes["id"].Value;
string enable = x.Attributes["enable"].Value;
string url = x.Attributes["url"].Value;
if (enable.Equals("On")
{
...
}
else
{
...
}
}
}
else
{
...
}
我只是想知道为什么enable属性不是布尔字段 – Shimmy 2009-11-23 02:30:34
另外,你使用什么语言,C#或VB? – Shimmy 2009-11-23 02:37:16
考虑,如果你已经使用了更高版本的.NET与VB你可以享受VB XML文本的东西:http://ookii.org/post/xml_literals_in_visual_basic_9.aspx – Shimmy 2009-11-23 02:38:59