2009-06-15 6 views
0

私は、各ドキュメントのフィールドマップを持つドキュメントのリストを含むXML構成ファイルを持っています。私はLINQ to XMLを使用して、ドキュメントをリスト<>構造の階層として表す内部データ構造を作成したいと考えています。一般的なList <>オブジェクトが1つのLINQクエリを使用して親構造と子構造に存在するXMLから親子データ構造をどのように埋め込むのですか?

フィールドを含めるかどうかは、Includeという属性によって決まります。以下は

class FieldMap 
{ 
    public string OldName { get; set; } 
    public string NewName { get; set; } 
} 

class Document 
{ 
    public string DocumentName { get; set; } 
    public List<FieldMap> FieldMaps; 
} 

private List<Document> Documents; 

私はそれがない持っているコードです:文書を表すデータ構造は、このようになります

<Documents> 
    <Document Include="1" DocumentName="Report1" > 
    <Field Include="1" OldName="Old1" NewName="New1"/> 
    <Field Include="1" OldName="Old2" NewName="New2"/> 
    <Field Include="0" OldName="Old3" NewName="New3"/> 
    </Document> 
    <Document Include="1" DocumentName="Report2" > 
    <Field Include="1" OldName="Old1" NewName="New1"/> 
    <Field Include="0" OldName="Old3" NewName="New3"/> 
    </Document> 
</Documents> 

:ここ

は、XMLがどのように見えるかのサンプルです最初の部分にドキュメントを配置してください。

var ds = from row in elem.Descendants("Document") 
     where row.Attribute("Include").Value == "1" 
     select new Document 
     { 
      DocumentName = row.Attribute("DocumentName").Value, 
     }; 

Documents = ds.ToList<Document>(); 

上記のコードを修正して、各ドキュメント内のリスト構造を作成したいと思います。私はドキュメントリスト<を繰り返し実行することでこれを行うことができ、ループ内で別のLINQクエリを実行することができますが、すべてを統合クエリとして行うことをお勧めします。

+0

1つのクエリですべてを実行するように促します。その(間違いなく)痛みを読み取る、それは間違いなくテストやデバッグの痛みです。 –

+0

@フランク、私は同意しない。この場合、サブクエリを使用することは理にかなっており、結果にforeachループを使用するだけでリストプロパティを設定するよりもきれいです。 foreachループがテスト/デバッグするのが簡単だと思う場合は、先に進んで使用してください。しかし、LINQのサブクラスを妨げる理由はありません。 – CoderDennis

+0

こんにちはフランク、私は下のAlberEinの答えに傾いています。私も上記のDennis Palmerに同意する傾向があります。それがきれいだと思うなら、あなたのアプローチを提供できますか? ありがとう –

答えて

2

あなたは次のようなことを試しましたか?

var ds = from row in elem.Descendants("Document") 
     where row.Attribute("Include").Value == "1" 
     select new Document 
     { 
      DocumentName = row.Attribute("DocumentName").Value, 
      FieldMaps = //Solution from here 
       (from field in row.Descendants ("Field") 
       where field.Attribute("Include").Value == "1" 
       select new FieldMap { 
        OldName = field.Attribute("OldName").Value, 
        NewName = field.Attribute("NewName").Value 
       }).ToList() //To here 
     }; 

Documents = ds.ToList<Document>(); 

一部の人々は、それはforeachのを使用してLINQクエリの外FielMapsプロパティをpopullateするために明確だと、私はそれに同意しないと言うが、あなたはその代わりのようにそれはのようなもののようになります場合:

var ds = from row in elem.Descendants("Document") 
     where row.Attribute("Include").Value == "1" 
     select new Document 
     { 
      DocumentName = row.Attribute("DocumentName").Value 
     }; 

Documents = ds.ToList<Document>(); 

foreach (var document in Documents) 
    document.FieldMaps = elem.Descendants("Document") 
     .Where(doc => doc.Attributes("DocumentName") == document.DocumentName) 
     .Select(doc => doc.Descendants("Fields")) 
     .Where(field => field.Attributes("Include").Value == "1") 
     .Select(field => new FieldMap 
      { 
       OldName = field.Attributes("OldName").Value, 
       newName = field.Attributes("NewName").Value 
      } 
     ).ToList(); 
+0

最初のアプローチはうまくいった。 –

関連する問題