2012-01-22 20 views
0

xID & &に基づいてノード「FieldName」を取得するには、LINQ TO XMLを使用します。 注:dIDが存在する場合と存在しない場合があります。 これまでに試したことは以下の通りです。 しかし、私のコードはLINQ TO XML複数の条件に基づいてノードを取得します

<Configuration> 
     <Contract xID="2"> 
      <Document dID="227"> 
       <FieldName name="AAAA"/> 
       <FieldName name="BBBB"/> 
      </Document> 
     </Contract> 
     <Contract xID="5"> 

       <FieldName nam`enter code here`e="CCCC"/> 
       <FieldName name="DDDD"/> 

     </Contract> 
    </Configuration> 

がどのような私は、これまでにしようとしたが、以下に印刷されて存在していませんでしたdoesnの条件を処理しません。私の入力はXID = 5であり、= 777をなかった場合:

XDocument xmlDoc = XDocument.Load("../../FieldConfiguration.xml"); 

    var fieldNames = (from n in xmlDoc.Descendants("Contract") 
         where (int)n.Attribute("xID") == 5 && 
          (int)n.Element("Document").Attribute("dID") == 227 
         from f in n.Element("Document").Elements() 
         select (string) f.Attribute("name")).ToList(); 

    foreach (string name in fieldNames) 
    { 
     Console.WriteLine("Site: " + name); 
    } 

答えて

0

ちょうどあなたのwhere句

XDocument xmlDoc = XDocument.Load("../../FieldConfiguration.xml"); 

    var fieldNames = (from n in xmlDoc.Descendants("Contract") 
         where (int)n.Attribute("xID") == 5 && 
          n.Element("Document").Attribute("dID") != null && 
(int)n.Element("Document").Attribute("dID") == 227 
          from f in n.Element("Document").Elements() 
          select (string) f.Attribute("name")).ToList(); 

    foreach (string name in fieldNames) 
    { 
     Console.WriteLine("Site: " + name); 
    } 
+0

ジェイソンにヌルのチェックを追加存在しませdoesnのなかったときに私のコードは条件を処理しないが その場合、結果セットには "CCCC"& "DDDD"が含まれている必要があります。これは無視する必要があります。 – user1163306

+0

where句に好きなだけ多くのチェックを追加し、括弧を使用して優先順位を指定します。 – Jason

+0

LINQ TO XML、だから私は "契約"ノードではないthr場合は、 "dID"を無視することはできません – user1163306

関連する問題