如何在 C# 中访问带有前缀或命名空间的 XML 元素
本文关键字:前缀 命名空间 元素 XML 访问 | 更新日期: 2023-09-27 18:36:29
嘿,任何人都可以帮助我...我也面临同样的问题,但给出的解决方案对我不起作用.我也用谷歌搜索了很多...但找不到任何解决方案...
我付出的努力,终于看---
protected void GetRSS(string url)
{
WebRequest MyRssRequest = WebRequest.Create(url);
WebResponse MyRssResponse = MyRssRequest.GetResponse();
Stream MyRssStream = MyRssResponse.GetResponseStream();
XmlDocument MyRssDocument = new XmlDocument();
MyRssDocument.Load(MyRssStream);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
XmlNamespaceManager nsmgrcontent = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
XmlNamespaceManager nsmgrwfw = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("wfw", "http://wellformedweb.org/CommentAPI/");
XmlNamespaceManager nsmgratom = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
XmlNamespaceManager nsmgrsy = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("sy", "http://purl.org/rss/1.0/modules/syndication/");
XmlNamespaceManager nsmgrslash = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("slash", "http://purl.org/rss/1.0/modules/slash/");
XmlNamespaceManager nsmgrgeorss = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("georss", "http://www.georss.org/georss");
XmlNamespaceManager nsmgrgeo = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
XmlNamespaceManager nsmgrmedia = new XmlNamespaceManager(MyRssDocument.NameTable);
nsmgr.AddNamespace("media", "http://search.yahoo.com/mrss/");
XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");
string sTitle = "";
string sLink = "";
string sDescription = "";
string sPubDate = "";
string sCreator = "";
// Iterate/Loop through RSS Feed items
for (int i = 0; i < MyRssList.Count; i++)
{
XmlNode MyRssDetail;
MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
if (MyRssDetail != null)
{
sTitle = MyRssDetail.InnerText;
}
else
{
sTitle = "";
}
MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
if (MyRssDetail != null)
{
sLink = MyRssDetail.InnerText;
}
else
{
sLink = "";
}
MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
if (MyRssDetail != null)
{
sDescription = MyRssDetail.InnerText;
}
else
{
sDescription = "";
}
MyRssDetail = MyRssList.Item(i).SelectSingleNode("pubDate");
if (MyRssDetail != null)
{
sPubDate = MyRssDetail.InnerText;
}
else
{
sPubDate = "";
}
MyRssDetail = MyRssList.Item(i).SelectSingleNode("creator", nsmgr);
if (MyRssDetail != null)
{
sCreator = MyRssDetail.InnerText;
}
else
{
sCreator = "";
}
string JournalistName = MyRssList.Item(i).SelectSingleNode("lastBuildDate").NextSibling.InnerText;
HtmlTableCell block = new HtmlTableCell();
// You can style the Title From Here
block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>";
HtmlTableRow row = new HtmlTableRow();
row.Cells.Add(block);
tbl_Feed_Reader.Rows.Add(row);
HtmlTableCell block_description = new HtmlTableCell();
//You can style the Description from here
block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
HtmlTableRow row2 = new HtmlTableRow();
row2.Cells.Add(block_description);
tbl_Feed_Reader.Rows.Add(row2);
HtmlTableCell block_pubdate = new HtmlTableCell();
//You can style the Description from here
block_pubdate.InnerHtml = "<p align='justify'>" + sPubDate + "</p>";
HtmlTableRow row3 = new HtmlTableRow();
row3.Cells.Add(block_pubdate);
tbl_Feed_Reader.Rows.Add(row3);
HtmlTableCell block_creator = new HtmlTableCell();
//You can style the Description from here
block_creator.InnerHtml = "<p align='justify'><b>" + JournalistName + "</b></p>";
HtmlTableRow row4 = new HtmlTableRow();
row4.Cells.Add(block_creator);
tbl_Feed_Reader.Rows.Add(row4);
}
}
下面给出的部分给我 null 作为元素......
MyRssDetail = MyRssList.Item(i).SelectSingleNode("creator", nsmgr);
if (MyRssDetail != null)
{
sCreator = MyRssDetail.InnerText;
}
else
{
sCreator = "";
}
如果"creator"位于具有命名空间的 XML 文件中的段中。
<widget xmlns="http://www.w3.org/2001/XMLSchema">
<creator>Widget Creator</creator>
</widget>
然后
nsmgr.AddNamespace("blah", "http://www.w3.org/2001/XMLSchema");
这变成了...
MyRssList.Item(i).SelectSingleNode("blah:creator", nsmgr);