2012-08-24 2 views
1

오른쪽 structyre에서 rss 문서에 노드를 추가하는 방법을 알아 냈습니다. 이제 pubDate 순서로 정렬 한 다음 화면에 출력해야합니다. 예제를 온라인으로 살펴보면 XDocument와 Linq는 많이 있지만 XmlDocument는 없다는 것을 알았습니다. 내가 갖고있는 코드를 스크랩해서 여기에있는 조언으로 XDocument에서 어떻게 할 것인지, 아니면 XMLDocument를 계속 사용하여 정렬 할 방법을 찾아 내는지에 대한 내 머리를 긁어 라.XMLdocument 정렬

XMLDocument를 사용하면 코드가 정확히 원하는대로 작동하므로 피드를 화면에 뱉어 내면 pubDate 순서로 정렬해야합니다. 그래서 나는 시간을두고이 사실을 고수 할 것입니다. 이 기사 http://support.microsoft.com/kb/555060과 xslt 누군가 Stack Overflow에 게시되었음을 발견했지만 코드에서 "XmlHelperFunctions"를 호출하는 방법을 모른다. XSLT는 내가 가진 가장 쉬운 옵션인가, 아니면 거기에 더 쉬운 것이 있습니까? 당신은 Linq to XML를 사용하여 매우 빠르고 손쉽게 수행 할 수 있습니다

<?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

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 피드 :

이 내 코드입니다.

XElement.Parse (...)를 사용하여 XML을 구문 분석하면 OrderBy 또는 OrderByDescending 함수를 사용하고 내용을 매우 쉽게 변경할 수 있습니다. 다음은 간단한 예입니다 :는 XML은 당신과 동일하지 않을

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 개체를 지정할 수 있습니다, 이것은 다음과 같은 출력 :이 유용 희망

<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, 감사합니다. .OrderBy를 사용하려고하는 intellisense에서 오류가 발생하는 것 같습니다. "... XElement에는 OrderBy에 대한 정의가 없습니다." – JK36

+0

나는 당신이 "System.Linq 사용"을 가지고 있는지 확인해야 할 것이라고 기대한다. 코드 파일의 맨 위에있는 명령문. 그게 대부분의 확장 메서드 (OrderBy 포함) – Balthy

+0

Hmmm을 포함하는 주요 Linq 네임 스페이스입니다 ... 난 그냥 기본 콘솔 응용 프로그램에서 썼습니다. 다음 내용을 모두 사용합니까? using System; using System.Linq; using System.Xml.Linq; – Balthy