簡體   English   中英

XML上的LINQ查詢

[英]LINQ query on XML

我想獲取app-type="applicant"( app-type="applicant-inventor" and designation="us-only" )中的applicant元素的名稱和地址。

我應如何嘗試使用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>

你可以試試看

 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;
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
            };

大聲笑我喜歡復雜的答案。

將xpath與XPathSelect結合使用...

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.

申請人元素,其中app-type =“ applicant”或(app-type =“ applicant-inventor”和designation =“ us-only”)

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()
     };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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