C# XML文档排序

C# XML文档排序,c#,xmldocument,C#,Xmldocument,我已经知道如何在正确的结构中将节点附加到rss文档中。我现在需要按pubDate顺序对其进行排序,然后输出到屏幕。通过在线查看这些示例,我发现了很多XDocument和Linq的东西,但没有找到XmlDocument。我在琢磨是放弃我拥有的代码,并根据这里的建议在XDocument中解决该如何做,还是继续使用XMLDocument并找出一种排序方法 有了XMLDocument,我就可以让代码完全按照我想要的方式工作,只需要在将提要输出到屏幕时按pubDate顺序对其进行排序。所以我想我会暂时坚持

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

有了XMLDocument,我就可以让代码完全按照我想要的方式工作,只需要在将提要输出到屏幕时按pubDate顺序对其进行排序。所以我想我会暂时坚持这一点。我发现有人在Stack Overflow中发布了这篇文章和一个xslt,但我不知道如何从代码中调用“XMLHelperFunction”。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);
XmlDocument xmlDoc=new XmlDocument();
LoadXml(rssFeed.ToString());
XmlNodeList nl=xmlDoc.SelectNodes(“/rss/channel/item”);
foreach(nl中的XmlNode xn)
{
字符串title=xn[“title”]。InnerText;
字符串链接=xn[“链接”]。InnerText;
字符串desc=xn[“description”]。InnerText;
字符串auth=xn[“author”]。InnerText;
字符串pdate=xn[“pubDate”]。InnerText;
XmlElement itemnode=xmlDoc.CreateElement(“项目”);
itemnode.InnerXml=“”;
itemnode[“title”]。InnerText=标题;
itemnode[“link”]。InnerText=link;
itemnode[“description”]。InnerText=desc;
itemnode[“author”]。InnerText=auth;
itemnode[“pubDate”]。InnerText=pdate;
xmlDoc.DocumentElement.SelectNodes(“/rss/channel”)[0].AppendChild(itemnode);
}
//输出到屏幕
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>

我的RSS提要
http://www.mylink.aspx
最高分数
http://www.mymarks.aspx
p此处的说明字段/p
维夫
2012年8月16日星期四12:10:54 GMT
咖世家咖啡
http://www.Costa.aspx
pCosta咖啡有特别优惠。/p
迈克
2012年8月23日星期四格林威治标准时间15:55:53
庆祝成功
http://www.Celebrate.aspx
请大家庆祝一下
维夫
2012年8月22日星期四09:10:21 GMT

您可以使用以下工具快速、轻松地完成此操作

如果使用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());
XElement元素=XElement.Parse(@)
");
变量元素=元素。元素(“通道”)
.要素(“项目”)
.OrderBy(e=>DateTime.ParseExact(e.Attribute(“pubDate”).Value,“dd/MM/yyyy”,null))
.选择(e=>新元素(“项目”,
新元素(“标题”,即属性(“标题”)。值),
新元素(“pubDate”,e.Attribute(“pubDate”).Value));//在这里做变换。
元素。元素(“通道”)。替换所有(元素);
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>

有些事
10/02/2010
有些事
22/01/2011
什么
24/03/2012
某物
22/11/2012

我希望这是有用的。

您可以使用它快速、轻松地完成这项工作

如果使用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());
XElement元素=XElement.Parse(@)
");
变量元素=元素。元素(“通道”)
.要素(“项目”)
.OrderBy(e=>DateTime.ParseExact(e.Attribute(“pubDate”).Value,“dd/MM/yyyy”,null))
.选择(e=>新元素(“项目”,
新元素(“标题”,即属性(“标题”)。值),
新元素(“pubDate”,e.Attribute(“pubDate”).Value));//在这里做变换。
元素。元素(“通道”)。替换所有(元素);
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>

有些事
10/02/2010
有些事
22/01/2011
什么
24/03/2012
某物
22/11/2012

我希望这是有用的。

看看这个看看这个你好,谢谢。我在尝试使用.OrderBy-“…XElement不包含OrderBy的定义”时,在intellisense中似乎遇到了一个错误。我希望您需要确保在代码文件的顶部有一个“using System.Linq;”语句。这是主要的Linq名称空间,其中包含大多数扩展方法(包括OrderBy)Hmmm。。。我只是在一个默认的控制台应用程序中写的。您是否有以下所有使用语句?使用制度;使用System.Linq;使用System.Xml.Linq;是的,我给你的代码和在.cs文件中键入的代码一模一样。Intellisense就是不喜欢这句台词“e=>DateTime.ParseExact…对不起,我想不出会出什么问题。您使用的是什么版本的VS和.NET?这是在2010年和4.0。您可以尝试从OrderBy方法中删除类型参数,因为它们可以隐式解析。或者你可以玩一下“按表情排序”游戏,看看你能不能让任何表情起作用……嗨,亲爱的,谢谢。我在尝试使用.OrderBy-“…XElement不包含OrderBy的定义”时,在intellisense中似乎遇到了一个错误。我希望您需要确保在顶部有一个“using System.Linq;”语句