2016-02-26 53 views
1

嘿,任何人都可以幫助我......我也面臨同樣的問題,但是給出的解決方案並不適合我。我也用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 = ""; 
} 

回答

0

如果「創建者」位於具有名稱空間的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); 
相關問題