C# SelectSingleNode 存在命名空间问题
我正在使用 C# (.NET 2.0) - 实际上试图让它在 Mac OS X 上使用 MONO 工作(我不认为 MONO 是问题)
给定以下 XML 片段,该片段已从更大的 XmlDocument 中作为 XmlNode 检索:
<subcategoryCode xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992">N100</subcategoryCode>
<subcategoryName xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992">DJ Headphones</subcategoryName>
<products xlink:href="tcm:5-33975" xlink:title="TESTONE Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
<products xlink:href="tcm:5-54295" xlink:title="HPX2000 Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
<products xlink:href="tcm:5-54296" xlink:title="HPX4000 Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
我正在尝试使用 SelectSingleNode 检索 subcategoryName 但我根本做不到。这是我的代码:
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(String.Empty, "uuid:7E1158D2-DA42-4048-8513-66B4D48FA992");
XmlNodeList subcatList = doc.GetElementsByTagName("subcategories");
foreach (XmlNode subcat in subcatList) {
html += "<div id=\"";
html += subcat.SelectSingleNode("subcategoryName", nsm).InnerText; // <-- HERE IS MY PROBLEM!!!
html += "\" class=\"product_thumbs_holder\" style=\"display: block; \">";
html += "</div>";
html += "<div style=\"clear:both\"></div>";
}
我相信这个问题可能与我处理命名空间的方式有关,但我已经在这个问题上坚持了几个小时了。我尝试了一堆类似的 AddNamespace 声明,但没有成功。
那里的任何人都可以提供任何指示来说明问题出在哪里吗?
I am using C# (.NET 2.0) -- actually trying to have it working on Mac OS X using MONO (I don't think MONO is the issue)
Given the following XML fragment which has been retrieved as a XmlNode from a bigger XmlDocument:
<subcategoryCode xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992">N100</subcategoryCode>
<subcategoryName xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992">DJ Headphones</subcategoryName>
<products xlink:href="tcm:5-33975" xlink:title="TESTONE Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
<products xlink:href="tcm:5-54295" xlink:title="HPX2000 Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
<products xlink:href="tcm:5-54296" xlink:title="HPX4000 Composition" xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="uuid:7E1158D2-DA42-4048-8513-66B4D48FA992" />
I am trying to retrieve subcategoryName using SelectSingleNode but I simply cannot. This is my code:
XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace(String.Empty, "uuid:7E1158D2-DA42-4048-8513-66B4D48FA992");
XmlNodeList subcatList = doc.GetElementsByTagName("subcategories");
foreach (XmlNode subcat in subcatList) {
html += "<div id=\"";
html += subcat.SelectSingleNode("subcategoryName", nsm).InnerText; // <-- HERE IS MY PROBLEM!!!
html += "\" class=\"product_thumbs_holder\" style=\"display: block; \">";
html += "</div>";
html += "<div style=\"clear:both\"></div>";
}
I believe the issue is probably related to the way I am handling the Namespace but I have been stuck on this for hours. I have tried a bunch of similar AddNamespace declarations with no luck.
Anyone out there would be kind enough to provide any pointers to where the issue is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XPath 不适用于默认名称空间。您必须为命名空间创建前缀。此代码应该可以工作:
注意:您不需要在 xml 文档中添加前缀。 xml 文档中的前缀和代码中的前缀不必匹配,只要关联的命名空间匹配即可。
XPath doesn't work with default namespaces. You must create a prefix for the namespace. This code should work:
Note: you don't need to add the prefix in de xml document. The prefixes in the xml document and the prefixes in the code don't have to match, as long as the associated namespaces match.
尝试直接获取
subcategoryName
:这将获取 xml 文件中的所有
subcategoryName
元素。Try getting the
subcategoryName
directly:This will get all of the
subcategoryName
elements in the xml file.