2012-08-24 37 views
1

我想出瞭如何將節點追加到我的rss文檔的右側結構中。我現在需要按pubDate順序排序,然後輸出到屏幕。看看網上的例子,我發現很多XDocument和Linq的東西,但沒有與XmlDocument。抓住我的頭是否要廢除我擁有的代碼,並在XDocument中使用此處的建議來解決該問題,或繼續使用XMLDocument並找出排序方式。XMLdocument Sort

使用XMLDocument我已經得到了完全按照我的想法工作的代碼,只需要按pubDate順序對其進行排序,然後再將它分發到屏幕上。所以我想我會堅持這一點的時間。我發現這篇文章http://support.microsoft.com/kb/555060和xslt有人張貼在堆棧溢出,但我不知道如何從我的代碼調用「XmlHelperFunctions」。 XSLT是我擁有的最簡單的選項,還是有更簡單的方法呢?

這是我的代碼:

XmlDocument xmlDoc = new XmlDocument(); 

    xmlDoc.LoadXml(rssFeed.ToString()); 

    XmlNodeList nl = xmlDoc.SelectNodes("/rss/channel/item"); 

    foreach (XmlNode xn in nl) 
    { 
     string title = xn["title"].InnerText; 
     string link = xn["link"].InnerText; 
     string desc = xn["description"].InnerText; 
     string auth = xn["author"].InnerText; 
     string pdate = xn["pubDate"].InnerText; 

     XmlElement itemnode = xmlDoc.CreateElement("item"); 

     itemnode.InnerXml = "<title></title><link></link><description></description><author></author><pubDate></pubDate>"; 
     itemnode["title"].InnerText = title; 
     itemnode["link"].InnerText = link; 
     itemnode["description"].InnerText = desc; 
     itemnode["author"].InnerText = auth; 
     itemnode["pubDate"].InnerText = pdate; 

     xmlDoc.DocumentElement.SelectNodes("/rss/channel")[0].AppendChild(itemnode); 
    } 

    // Output to screen 
    xmlDoc.Save(Response.Output); 

我的RSS提要

<?xml version="1.0" encoding="utf-8"?> 
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 
<channel> 
<title>My RSS Feed</title> 
<link>http://www.mylink.aspx</link> 
<description> 
</description> 
<item> 
    <title>Top marks</title> 
    <link>http://www.mymarks.aspx</link> 
    <description> 
    &lt;p&gt;description field here&lt;/p&gt; 
    </description> 
    <author>Viv</author> 
    <pubDate>Thu, 16 Aug 2012 12:10:54 GMT</pubDate> 
</item> 
<item> 
    <title>Costa Coffee</title> 
    <link>http://www.Costa.aspx</link> 
    <description> 
    &lt;p&gt;Costa Coffee have special offers.&lt;/p&gt; 
    </description> 
    <author>Mike</author> 
    <pubDate>Thu, 23 Aug 2012 15:55:53 GMT</pubDate> 
</item> 
<item> 
    <title>Celebrate success</title> 
    <link>http://www.Celebrate.aspx</link> 
    <description> 
    &lt;p&gt;Lets all celebrate &lt;/p&gt; 
    </description> 
    <author>Viv</author> 
    <pubDate>Thu, 22 Aug 2012 09:10:21 GMT</pubDate> 
</item> 
</channel> 
</rss> 
+0

看看這個http://stackoverflow.com/questions/344737/sorting-xml-nodes-based-on-datetime-attribute-c-xpath – saj

回答

3

你可以做到這一點相當快速,輕鬆地使用Linq to XML

如果您使用XElement.Parse(...)解析XML,則可以使用OrderBy或OrderByDescending函數並輕鬆更改內容。 下面是一個簡單的例子:

XElement element = XElement.Parse(@" 
<rss> 
<channel> 
<item title='something' pubDate='22/11/2012'/> 
<item title='something2' pubDate='24/03/2012'/> 
<item title='something3' pubDate='10/02/2010'/> 
<item title='something4' pubDate='22/01/2011'/> 
</channel> 
</rss>"); 

var elements = element.Element("channel") 
       .Elements("item") 
       .OrderBy<XElement, DateTime>(e => DateTime.ParseExact(e.Attribute("pubDate").Value, "dd/MM/yyyy", null)) 
       .Select(e => new XElement("item", 
        new XElement("title", e.Attribute("title").Value), 
        new XElement("pubDate", e.Attribute("pubDate").Value))); // Do transform here. 

      element.Element("channel").ReplaceAll(elements); 

      Console.Write(element.ToString()); 

的XML不會是你的一樣,但希望它給你的,你可以做些什麼的想法。您只需指定的XElement和XAttribute對象作爲新的XML內容,此輸出以下:

<rss> 
    <channel> 
    <item> 
     <title>something3</title> 
     <pubDate>10/02/2010</pubDate> 
    </item> 
    <item> 
     <title>something4</title> 
     <pubDate>22/01/2011</pubDate> 
    </item> 
    <item> 
     <title>something2</title> 
     <pubDate>24/03/2012</pubDate> 
    </item> 
    <item> 
     <title>something</title> 
     <pubDate>22/11/2012</pubDate> 
    </item> 
    </channel> 
</rss> 

我希望這是有益的。

+0

嗨Balthy,謝謝。我似乎在intellisense中嘗試使用.OrderBy時出現錯誤 - 「.... XElement不包含OrderBy的定義」。 – JK36

+0

我期望這將是你需要確保你有一個「使用System.Linq;」聲明在代碼文件的頂部。 這是包含大部分擴展方法(包括OrderBy)的主要Linq命名空間 – Balthy

+0

嗯......我剛剛在一個默認的控制檯應用程序中寫道。 你有以下所有使用語句嗎?使用系統的 ;使用System.Linq的 ; using System.Xml.Linq; – Balthy