【发布时间】:2021-04-27 21:35:51
【问题描述】:
我正在尝试按优先级节点对下面的 XML 进行排序,因此 1.0 是最高的,然后它们紧随其后。但是我尝试的一切都不会返回任何东西。这是我的代码,第一个块是我所做的尝试,它只是空的,第二个块是我的内联尝试,它返回一个空引用。
第一:
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
IEnumerable<XElement> list =
from el in xelementList1.Elements(ns + "url")
let priority = (int)el.Element(ns + "priority")
orderby priority descending
select el;
第二:
XDocument xdocument = this.BuildXmlDocument((IEnumerable<XElement>)xelementList1.OrderByDescending(x => x.Element("priority")?.Value));
文档:
<urlset xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https:///</loc>
<lastmod>2021-03-15</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///404/</loc>
<lastmod>2021-04-22</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///500/</loc>
<lastmod>2021-04-22</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>https:///avalon-brochures/</loc>
<lastmod>2021-04-27</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https:///contact-us/</loc>
<lastmod>2021-04-27</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https:///cors-test/</loc>
<lastmod>2021-03-15</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
【问题讨论】:
-
如果你在没有排序的情况下运行你的代码会返回任何东西吗?