我已经知道如何在正确的结构中将节点附加到我的rss文档。我现在需要按pubDate顺序对其进行排序,然后输出到屏幕。查看在线示例,我发现了很多XDocument和Linq的东西,但XmlDocument却一无所获。我在挠头,是放弃我已有的代码,并根据这里的建议在XDocument中解决如何做到这一点,还是继续使用XMLDocument并找到排序的方法。
有了XMLDocument,我的代码就像我想要的那样工作了,只需要我的提要在输出到屏幕时按pubDate顺序排序即可。所以我想我会暂时坚持这一点。我在Stack Overflow中找到了这篇文章的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>
<p>description field here</p>
</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>
<p>Costa Coffee have special offers.</p>
</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>
<p>Lets all celebrate </p>
</description>
<author>Viv</author>
<pubDate>Thu, 22 Aug 2012 09:10:21 GMT</pubDate>
</item>
</channel>
</rss>发布于 2012-08-24 18:33:13
您可以使用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>我希望这是有用的。
https://stackoverflow.com/questions/12107097
复制相似问题