简体   繁体   English

XML上的LINQ查询

[英]LINQ query on XML

I want to get the name and address of the applicant element where app-type="applicant" or ( app-type="applicant-inventor" and designation="us-only" ) . 我想获取app-type="applicant"( app-type="applicant-inventor" and designation="us-only" )中的applicant元素的名称和地址。

How should I try to query with 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>

You may try this, 你可以试试看

 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");

Above query will return the address on this format: 上面的查询将以这种格式返回地址:

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

But you'll probably want to build an Address like this (needs more work, but this is the idea): 但是您可能想要构建一个这样的地址(需要做更多的工作,但这是个主意):

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

Lol i like the complicated answers. 大声笑我喜欢复杂的答案。

Use xpath with XPathSelect instead ... 将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.

applicant element where app-type="applicant" or ( app-type="applicant-inventor" and designation="us-only" ) 申请人元素,其中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