2011-11-18 10 views
0

または(app-type="applicant-inventor" and designation="us-only")という名前のapplicant要素の名前と住所を取得したいとします。XMLのLINQクエリ

LINQを使用してクエリを実行する方法を教えてください。あなたはこれを試すことが

<applicants> 
    <applicant designation="all-except-us" app-type="applicant" sequence="1"> 
    <addressbook> 
     <name name-type="legal">Hello LIGHTING CO., LTD.</name> 
     <address> 
      <address-1>Myanmar</address-1> 
     </address> 
    </addressbook> 
    <nationality> 
     <country>CN</country> 
    </nationality> 
    <residence> 
     <country>CN</country> 
    </residence> 
    </applicant> 
    <applicant designation="us-only" app-type="applicant-inventor" sequence="2"> 
    <addressbook> 
     <name name-type="natural">Henry </name> 
     <address> 
      <address-1>Chicago 380892</address-1> 
     </address> 
    </addressbook> 
    <nationality> 
     <country>CN</country> 
    </nationality> 
    <residence> 
     <country>CN</country> 
    </residence> 
    </applicant> 
    <applicant designation="us-only" app-type="applicant-inventor" sequence="3"> 
    <addressbook lang="EN"> 
     <name name-type="natural">Gho Chi</name> 
     <address> 
      <address-1>Thai 310012</address-1> 
     </address> 
    </addressbook> 
    <nationality> 
     <country>CN</country> 
    </nationality> 
    <residence> 
     <country>CN</country> 
    </residence> 
    </applicant> 
</applicants> 
+3

何を試しましたか?これは、あなたがドキュメントを読んだら簡単な質問でなければなりません。どこにこだわっていますか?この情報を追加するには、質問を編集してください。 –

+0

ここに私が言及していた "ドキュメント"のいくつかがあります:http://msdn.microsoft.com/en-us/vstudio/bb688087 –

+0

@Merlyn - >事実、私はLINQを勉強して、 。属性orとandのステートメントは私を混乱させます。 文書のThx。 – kevin

答えて

1
var query = from c in xdoc.Descendants("applicant") 
       where (c.Attribute("app-type").Value=="applicant" || 
       c.Attribute("app-type").Value=="applicant-inventor") && 
       c.Attribute("designation").Value=="us-only" 
       select c.Descendants("addressbook"); 

<addressbook> 
    <name name-type="natural">Henry </name> 
    <address> 
    <address-1>Chicago 380892</address-1> 
    </address> 
</addressbook> 

しかし、あなたは、おそらくのようなアドレスを構築したいと思いますこれは(より多くの作業が必要ですが、これがアイデアです)

var query = from c in xdoc.Descendants("applicant") 
      where (c.Attribute("app-type").Value=="applicant" || 
      c.Attribute("app-type").Value=="applicant-inventor") && 
      c.Attribute("designation").Value=="us-only" 
      select new { 
      Name= c.Descendants("addressbook").Descendants("name").First().Value, 
      Address=c.Descendants("addressbook").Descendants("address").First().Value, 
      Country = c.Descendants("residence").Descendants("country").First().Value 
      }; 
+0

あなたの完全な回答をありがとうございます! – kevin

+0

アドレス要素は必ず存在しません。それが存在しないとき、それは私に "NullReferenceException"を与えました。私はそれをどのように扱うべきですか?おかげさまで – kevin

+0

私はFirstOrDefault()を試しても、まだ私に例外を与えました。 – kevin

2

var result = from ele in xmlDoc.Descendants("applicant") 
    where ((string)ele.Attribute("app-type")) == "applicant" || 
    (((string)ele.Attribute("app-type")) == "applicant-inventor" && 
      ((string)ele.Attribute("designation")) == "us-only") 
        select ele; 
この形式上のアドレスを返しますクエリの上
+0

ありがとうございました!!! – kevin

1

私は複雑な答えが好きです。

XPathSelectで使用したXPathの代わりに...

use System.Xml.XPath; //Contains extensions for LINQ to XML 
.... 
var resultList = XPathSelectElements(XNode, String); 
... 
var resultElement = XPathSelectElement(XNode, String); 
//This can only select an element, not an attribute. 

申請要素アプリ型=「申請者」または(アプリ型=「申請者・発明者」と呼称=「私たち専用」)

var xpath = "/applicant[app-type=applicant or (app-type=\"applicant-inventor\" and designation=us-only)]/addressbook"; 
var resultList = XPathSelectElements(root, xpath); 

var nameAndAddress = 
    from el in resultList 
    select new { 
     addresses = el.Element("Address").Elements() 
     name = (string)el.Elements("name").FirstOrFDefault() 
    };