如何从 XmlDocument 中选择具有 XML 命名空间的 XML 节点?

发布于 2024-10-11 10:19:16 字数 875 浏览 7 评论 0原文

我的代码尝试从网站的 RSS 提要中获取数据。它可以很好地抓取节点,但是当尝试从带有冒号的节点抓取数据时,它会崩溃并给出错误“需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。”代码如下所示:

WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true");
WebResponse response = request.GetResponse();

StringBuilder sb = new StringBuilder("");
System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

for (int i = 0; i < 5; i++)
{
   XmlNode rssDetail;
   rssDetail = rssItems.Item(i).SelectSingleNode("dc:creator");

   if (rssDetail != null)
   {
      user = rssDetail.InnerText;
   }
   else
   {
      user = "";
   }
}

我知道我需要定义名称空间,但不确定如何执行此操作。如有帮助,将不胜感激。

my code attempts to grab data from the RSS feed of a website. It grabs the nodes fine, but when attempting to grab the data from a node with a colon, it crashes and gives the error "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." The code is shown below:

WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true");
WebResponse response = request.GetResponse();

StringBuilder sb = new StringBuilder("");
System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

for (int i = 0; i < 5; i++)
{
   XmlNode rssDetail;
   rssDetail = rssItems.Item(i).SelectSingleNode("dc:creator");

   if (rssDetail != null)
   {
      user = rssDetail.InnerText;
   }
   else
   {
      user = "";
   }
}

I understand that I need to define the namespace, but am unsure how to do this. Help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

南烟 2024-10-18 10:19:16

您必须使用 XmlNamespaceManagerdc 命名空间前缀a> 然后才能在 XPath 表达式中使用它:

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++) {
    XmlNode rssDetail = rssItems[i].SelectSingleNode("dc:creator", nsmgr);
    if (rssDetail != null) {
        user = rssDetail.InnerText;
    } else {
        user = "";
    }
}

You have to declare the dc namespace prefix using an XmlNamespaceManager before you can use it in XPath expressions:

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++) {
    XmlNode rssDetail = rssItems[i].SelectSingleNode("dc:creator", nsmgr);
    if (rssDetail != null) {
        user = rssDetail.InnerText;
    } else {
        user = "";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文