如何在c中使用前缀或命名空间来访问xml元素#
问题描述:
嘿,任何人都可以帮助我......我也面临同样的问题,但是给出的解决方案并不适合我。我也用Google搜索了这么多......但无法找到任何解决方案...如何在c中使用前缀或命名空间来访问xml元素#
的努力,我做了,最后的样子---
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 = "";
}
答
如果“创建者”位于具有名称空间的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);