在使用名称空间管理器时,我到处查看并且无法读取具有名称空间前缀的XML节点的值。
XmlDocument rssXmlDoc = new XmlDocument();
rssXmlDoc.Load("https://www.kijiji.ca/rss-srp-batterie-percussion/quebec/c612l9001");
StringBuilder rssContent = new StringBuilder();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssXmlDoc.NameTable);
nsmgr.AddNamespace("dc", @"http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-08-25T08-37-41");
nsmgr.AddNamespace("g-core", @"http://schemas.microsoft.com/office/infopath/2003/myXSD");
XmlNodeList rssNodes = rssXmlDoc.SelectNodes("rss/channel/item");
// Iterate through the items in the RSS file
foreach (XmlNode rssNode in rssNodes)
{
String itemsForList = "";
XmlNode rssSubNodeTitle = rssNode.SelectSingleNode("title");
XmlNode rssSubNodePrice = rssNode.SelectSingleNode("g-core:price", nsmgr);
if (rssSubNodeTitle != null && rssSubNodeTitle.InnerText != "")
{
itemsForList = rssSubNodeTitle.InnerText;
if (rssSubNodePrice != null) { itemsForList += " (" + rssSubNodePrice.InnerText + ")"; }
}
listBox1.Items.Add(WebUtility.HtmlDecode(itemsForList));
}
答案 0 :(得分:0)
g-core名称空间的URI错误。更正以下内容后,它对我有用:
nsmgr.AddNamespace("g-core", @"http://base.google.com/ns/1.0");
调试这些东西时,将XML保存到本地文件并在Visual Studio,代码等中检查它很有帮助。