C# SelectSingleNode 存在命名空间问题

发布于 2024-10-26 21:46:18 字数 1701 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆伤 2024-11-02 21:46:19

XPath 不适用于默认名称空间。您必须为命名空间创建前缀。此代码应该可以工作:

XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("x", "uuid:7E1158D2-DA42-4048-8513-66B4D48FA992");

XmlNodeList subcatList = doc.GetElementsByTagName("subcategories");

foreach (XmlNode subcat in subcatList) {
    html += "<div id=\"";
    html += subcat.SelectSingleNode("x:subcategoryName", nsm).InnerText; // <-- HERE IS MY PROBLEM!!!
    html += "\" class=\"product_thumbs_holder\" style=\"display: block; \">";
    html += "</div>";
    html += "<div style=\"clear:both\"></div>";
}

注意:您不需要在 xml 文档中添加前缀。 xml 文档中的前缀和代码中的前缀不必匹配,只要关联的命名空间匹配即可。

XPath doesn't work with default namespaces. You must create a prefix for the namespace. This code should work:

XmlNamespaceManager nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("x", "uuid:7E1158D2-DA42-4048-8513-66B4D48FA992");

XmlNodeList subcatList = doc.GetElementsByTagName("subcategories");

foreach (XmlNode subcat in subcatList) {
    html += "<div id=\"";
    html += subcat.SelectSingleNode("x:subcategoryName", nsm).InnerText; // <-- HERE IS MY PROBLEM!!!
    html += "\" class=\"product_thumbs_holder\" style=\"display: block; \">";
    html += "</div>";
    html += "<div style=\"clear:both\"></div>";
}

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.

青瓷清茶倾城歌 2024-11-02 21:46:19

尝试直接获取 subcategoryName

XmlNodeList subcatList = xmldoc.GetElementsByTagName( "subcategoryName" );
foreach( XmlNode subcat in subcatList )
{
    Console.WriteLine( subcat.InnerText );
}

这将获取 xml 文件中的所有 subcategoryName 元素。

Try getting the subcategoryName directly:

XmlNodeList subcatList = xmldoc.GetElementsByTagName( "subcategoryName" );
foreach( XmlNode subcat in subcatList )
{
    Console.WriteLine( subcat.InnerText );
}

This will get all of the subcategoryName elements in the xml file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文