[英]Unique nodes and c#
在c#中,有没有什么好方法可以使用DOM浏览XML节点列表,并获得仅包含唯一节点的节点列表以及每个节点的唯一可能属性的列表。
有问题的XMl文件具有相同名称但具有不同属性的节点,我希望列出所有可能的节点。 另外,我希望仅是唯一节点中的节点列表,而不是具有重复节点(因此,我目前生成的节点列表可能有两次联系,其中有3次联系)。 它需要适用于任何XML文档。 有任何想法吗?
这是一个例子:
<book id="bk112">
<author>Galos, Mike</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
</book>
<book id="bk162">
<genre>fiction</genre>
<popularity>High</popularity>
<price>20.00</price>
<publish_date>2002-03-12</publish_date>
</book>
<cd id="bk162">
<genre>jaz</genre>
<popularity>High</popularity>
<price>10.00</price>
</cd>
并获得某种输出,例如:
there are 2 of the type book
there are 1 of the type cd
there are 3 of the type genre
book may have the attributes author, title, genre, price, popularity, publish_date
但以适用于任何xml文件的方式。
对于流派,它不需要任何方式,只要知道文档中有3个流派节点即可。
这样可以吗?
XDocument xDoc = XDocument.Load("XMLFile1.xml");
List<XElement> distinctDocs = xDoc.Descendants().GroupBy(x => x.Name).Where(x => x.Count() == 1).Select(g => g.Single()).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.