C# 如何从maximumn属性到minimum属性读取XmlDocument?
我正在读dotnet/c#中的XmlDocument,通过使用System.Xml,我喜欢从多属性到少属性读取xmlement,如何读取?我们可以这样做吗 我的示例xml文件和编码:C# 如何从maximumn属性到minimum属性读取XmlDocument?,c#,.net,xml,linq,system.xml,C#,.net,Xml,Linq,System.xml,我正在读dotnet/c#中的XmlDocument,通过使用System.Xml,我喜欢从多属性到少属性读取xmlement,如何读取?我们可以这样做吗 我的示例xml文件和编码: <conditions><condition if:size="10pt" if:name="courier"/> <condition if:size="10pt"/> <condition if:size="10pt" if:name="times" ifnot:emp
<conditions><condition if:size="10pt" if:name="courier"/>
<condition if:size="10pt"/>
<condition if:size="10pt" if:name="times" ifnot:emphasis="bold"/></conditions>
foreach (XmlElement CondNode in XmlDoc.SelectNodes("//condition"))
{
//how to read and sort(not by length) by no. of attribute
}
foreach(XmlDoc.SelectNodes(//条件)中的xmlement CondNode)
{
//如何按属性号读取和排序(而不是按长度)
}
我希望按顺序阅读以下内容:
<condition if:size="10pt" if:name="times" ifnot:emphasis="bold"/>
<condition if:size="10pt" if:name="courier"/>
<condition if:size="10pt"/>
提前感谢,
Saran使用Linq到XML
XDocument doc = XDocument.Parse(xml);
var sorted = doc.Descendants("condition").OrderByDescending(node => node.Attributes().Count());
foreach (XElement condition in sorted)
{
// Do whatever you need
}
如果要继续使用XmlDocument,可以对节点进行如下排序:
var nodes = doc.SelectNodes("//condition")
.OfType<XmlElement>()
.OrderByDescending(x => x.Attributes.Count);
foreach (XmlElement CondNode in nodes)
{
//how to read and sort(not by length) by no. of attribute
}
var nodes=doc.SelectNodes(//条件”)
第()类
.OrderByDescending(x=>x.Attributes.Count);
foreach(节点中的XmlElement CondNode)
{
//如何按属性号读取和排序(而不是按长度)
}
通过使用,可以从集合中检索所有XmlElements(这应该包括集合中的所有节点),并接收一个
IEnumerable
作为结果。您可以将其用作Linq查询的起点。只实现了IEnumerable
的非泛型版本,因此您无法对其运行Linq查询,因为大多数方法都是IEnumerable
的扩展方法Hi Markus,非常感谢您提供的巨大帮助和我所期望的准确信息Hi Markus,我还有一个疑问,我能否仅计算名称空间属性以进行排序?thanks@saravanans:如果排序时只想使用特定的命名空间前缀,可以尝试以下操作:.OrderByDescending(x=>x.Attributes.OfType()。其中(a=>a.prefix==“if”).Count()代码>谢谢你给我宝贵的时间!工作正常。嗨,普约特,非常感谢你的大力帮助和我的期望