0

DOMを使用してXMLノードリストを調べ、一意のノードのみのノードリストと、各ノードの一意の可能な属性のリストを取得する良い方法はc#にありますか?

問題の XMl ファイルには、同じ名前のノードがありますが、属性が異なります。考えられるすべてのノードのリストが必要です。また、ノードのリストは、繰り返しを持つのではなく、一意のノードのみになりたいと考えています(したがって、現時点で生成するノードリストは、その中に2回、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 つのジャンル ノードがあることを知っておいてください。

4

1 に答える 1

0

これでうまくいくでしょうか?

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();
于 2013-08-22T15:14:42.863 に答える