首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XMLdocument排序

XMLdocument排序
EN

Stack Overflow用户
提问于 2012-08-24 17:55:46
回答 1查看 6.3K关注 0票数 2

我已经知道如何在正确的结构中将节点附加到我的rss文档。我现在需要按pubDate顺序对其进行排序,然后输出到屏幕。查看在线示例,我发现了很多XDocument和Linq的东西,但XmlDocument却一无所获。我在挠头,是放弃我已有的代码,并根据这里的建议在XDocument中解决如何做到这一点,还是继续使用XMLDocument并找到排序的方法。

有了XMLDocument,我的代码就像我想要的那样工作了,只需要我的提要在输出到屏幕时按pubDate顺序排序即可。所以我想我会暂时坚持这一点。我在Stack Overflow中找到了这篇文章的http://support.microsoft.com/kb/555060和一个xslt,但我不知道如何从我的代码中调用"XmlHelperFunctions“。XSLT是我拥有的最简单的选择吗,还是有更简单的选择?

这是我的代码:

代码语言:javascript
复制
    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源

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-24 18:33:13

您可以使用Linq to XML快速、轻松地完成此操作。

如果您使用XElement.Parse(...)解析XML然后,您可以使用OrderBy或OrderByDescending函数并很容易地更改内容。下面是一个简化的示例:

代码语言:javascript
复制
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的内容,这将输出以下内容:

代码语言:javascript
复制
<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>

我希望这是有用的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12107097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档