簡體   English   中英

唯一節點和C#

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM