[英]Retrieve XPath of an XML element by using its value
我的XmlFile看起来像这样:
<?xml version="1.0"?> <document-inquiry> <publication-reference data-format="docdb" xmlns="http://www.epo.org/exchange"> <document-id> <country>EP</country> <doc-number>2160088</doc-number> <kind>A1</kind> </document-id> </publication-reference>
</document-inquiry>
对于上述xml,我需要获取特定元素的xpath,例如说“ country element”作为
我的输出: “ / document-inquiry / publication-reference / document-id / country”
我的输入:使用其值“ EP”
这是我尝试的代码
doc.SelectSingleNode("/document-inquiry/publication-reference/document-id[text()='EP']");
我收到上述代码的null。
我必须使用C#代码来获取它。 有人可以帮我吗
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
class Program
{
static void Main()
{
var doc = XDocument.Load("D:\\xml\\neo.xml");
var ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("ns", "http://www.epo.org/exchange");
var elem = XDocument.Load("D:\\xml\\neo.xml")
.XPathSelectElement("//ns:document-id[ns:doc-number='1000']", ns);
if (elem != null)
{
Console.WriteLine(elem.ToString());
Console.ReadLine();
}
}
}
这对我来说非常合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.