2011-11-18 3 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() 
    };