2012-08-23 26 views
0

我有兩個rss提要我想合併在一起做出一個。實際上,我已經設法將兩個提要合併在一起,並將項目放在正確的位置 - 但每個屬性的數據,即標題包含標題+鏈接+描述+作者+ pubDate - 並重復鏈接,描述,作者和出版日期。有人可以幫我把它翻開嗎?結合兩個XMLDocuments rss

Object rssData = new object(); 
    Cms.UI.CommonUI.ApplicationAPI AppAPI = new Cms.UI.CommonUI.ApplicationAPI(); 
    rssData = AppAPI.ecmRssSummary(50, true, "DateCreated", 0, ""); 

    Response.ContentType = "text/xml"; 
    Response.ContentEncoding = System.Text.Encoding.UTF8; 

    XmlDocument xmlDocument = new XmlDocument(); 

    xmlDocument.LoadXml(rssData.ToString()); 


    //************************************************************ 
    // Obtain 5 data items from second list 

    Object rssData1 = new object(); 
    Cms.UI.CommonUI.ApplicationAPI AppAPI1 = new Cms.UI.CommonUI.ApplicationAPI(); 
    rssData1 = AppAPI1.ecmRssSummary(60, true, "DateCreated", 5, ""); 

    XmlDocument xmlDocument1 = new XmlDocument(); 

    xmlDocument1.LoadXml(rssData1.ToString()); 


    XmlNodeList nl = xmlDocument1.SelectNodes("/rss/channel"); 
    XmlNode root = nl[0]; //do I need this line? 

    foreach (XmlNode xnode1 in root.ChildNodes) 
    { 
     string title = xnode1.InnerText; 
     string link = xnode1.InnerText; 
     string desc = xnode1.InnerText; 
     string auth = xnode1.InnerText; 
     string pdate = xnode1.InnerText; 

     //Merge new nodes 

     node = xmlDocument.CreateNode(XmlNodeType.Element, "item", null); 
     //node.InnerText = "this is new node"; 

     //create title node 
     XmlNode nodeTitle = xmlDocument.CreateElement("title"); 
     nodeTitle.InnerText = title; 

     //create Link node 
     XmlNode nodeLink = xmlDocument.CreateElement("link"); 
     nodeLink.InnerText = link; 

     XmlNode nodeDesc = xmlDocument.CreateElement("description"); 
     nodeDesc.InnerText = desc; 

     XmlNode nodeAuthor = xmlDocument.CreateElement("author"); 
     nodeAuthor.InnerText = auth; 

     XmlNode nodepubDate = xmlDocument.CreateElement("pubDate"); 
     nodepubDate.InnerText = pdate; 


     //add to parent node 
     node.AppendChild(nodeTitle); 
     node.AppendChild(nodeLink); 
     node.AppendChild(nodeDesc); 
     node.AppendChild(nodeAuthor); 
     node.AppendChild(nodepubDate); 

     //add to elements collection 
     //xmlDocument.DocumentElement.AppendChild(node); 
     xmlDocument.DocumentElement.SelectNodes("/rss/channel")[0].AppendChild(node); 
    } 

    //******************************************** 

    xmlDocument.Save(Response.Output); 
+0

看起來問題是,當我定義變量的值時:string title = xnode1.InnerText; - 無法弄清楚如何獲得標題。 – JK36

回答

0

如您確實懷疑,問題就在這裏:

string title = xnode1.InnerText; 
string link = xnode1.InnerText; 
string desc = xnode1.InnerText; 
string auth = xnode1.InnerText; 
string pdate = xnode1.InnerText; 

難怪

在每個屬性的數據,即標題中包含 標題+鏈接+說明書+作者+ pubDate - 並重復鏈接, 描述,作者和pubdate。

您需要閱讀每個節點的特定子元素的值。大概是這樣的:

string title = xnode1["title"].InnerText; 

參考見http://www.csharp-examples.net/xml-nodes-by-name/

0

Konrad的回答很明確,但如果標題標籤內有嵌套標籤,則可能無法正常工作。

說,<title> TEXT <tag1> OTHER_TEXT </tag1></title> 在我的電腦上,它會返回一個TEXT和OTHER_TEXT的串聯。

這應該工作:

string title = xnode1["title"].FirstChild.Value; 

則firstChild將讓你的TEXT不管是否有其他標籤的標題或不是。

+0

我可能是錯的,但我認爲RSS是一種標準化的格式(因此'title'元素不會包含嵌套標籤) –