【发布时间】:2020-05-07 02:52:09
【问题描述】:
我有以下 XML:
<?xml version="1.0" encoding="UTF-8" ?>
<bookstore xmlns="urn:hl7-org:v3" xmlns:voc="urn:hl7-org:v3/voc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hl7-org:v3 PORT_MT020001.xsd" type="Observation" classCode="OBS" moodCode="EVN">
<book>
<title lang="en">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="en">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
当我尝试像这样在上述 xml 上使用 XMLDocument.SelectNodes() 时:
XmlNodeList xmlNodelist = doc.SelectNodes("//book");
Console.WriteLine(xmlNodelist.Count);
我得到以下结果:
0
当我将根节点中的xmlns 属性值更改为空时,如下所示:
<bookstore xmlns="" ...........>
然后我得到正确的结果:
2
为什么会这样?根节点中的xmlns 属性值对我来说至关重要。这个问题有解决办法吗?
【问题讨论】:
-
这个问题已经被问和回答了 1755 次(搜索“XPath Default Namespace”)。我随意选择了其中一个问题作为重复问题,因为它的支持率最高。
标签: c# xml xpath xml-parsing hl7-v3