对 XMLDocument.SelectNodes 结果进行排序
我有以下 XML,我想按优先级排序。不喜欢使用 XPath,并认为 LINQ 可能会工作,但我收到抛出参数异常,并显示消息“至少一个对象必须实现 IComparable”。
我可以以某种方式订购结果吗?
<Root>
<Item>
<Priority><![CDATA[4]]</Priority>
</Item>
<Item>
<Priority><![CDATA[1]]</Priority>
</Item>
<Item>
<Priority><![CDATA[3]]</Priority>
</Item>
</Root>
var result = doc.SelectNodes("//Item");
var ordered = ddddd.Cast<XmlNode>().OrderBy(x => x.SelectSingleNode("Priority")); //EXCEPTION!!!
I have the following XML which I want to order by priority. Don't fancy using XPath and thought LINQ might work but I get an argument exception thrown with the message "At least one object must implement IComparable."
Can I order the results somehow?
<Root>
<Item>
<Priority><![CDATA[4]]</Priority>
</Item>
<Item>
<Priority><![CDATA[1]]</Priority>
</Item>
<Item>
<Priority><![CDATA[3]]</Priority>
</Item>
</Root>
var result = doc.SelectNodes("//Item");
var ordered = ddddd.Cast<XmlNode>().OrderBy(x => x.SelectSingleNode("Priority")); //EXCEPTION!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
x => x.SelectSingleNode("优先级")
到x =>; x.SelectSingleNode("Priority").InnerText
哦,请记住,您将比较字符串,而不是整数,这样。
Change
x => x.SelectSingleNode("Priority")
tox => x.SelectSingleNode("Priority").InnerText
Oh, and remember, that you will compare strings, not ints, that way.