简体   繁体   中英

Simple Linq to XML question

Given the XML below..

and given that I have two variable 'Idnt' and 'Xref' which will store the # ID .. How do I get those values ?

I want

var Idnt = 5169452
and
var xref = 5169452




 <ecf:EntityPerson xmlns:ecf="xx">
  <nc:PersonName xmlns:nc="xx">
    <nc:PersonGivenName>JAMES</nc:PersonGivenName>
    <nc:PersonMiddleName>TIBERIUS</nc:PersonMiddleName>
    <nc:PersonSurName>KIRK</nc:PersonSurName>
  </nc:PersonName>
  <nc:PersonOtherIdentification xmlns:nc="xx">
    <nc:IdentificationID>5169452</nc:IdentificationID>
    <nc:IdentificationCategoryText>IDNT</nc:IdentificationCategoryText>
  </nc:PersonOtherIdentification>
  <nc:PersonOtherIdentification xmlns:nc="xx">
    <nc:IdentificationID>5169452</nc:IdentificationID>
    <nc:IdentificationCategoryText>XREF</nc:IdentificationCategoryText>
  </nc:PersonOtherIdentification>
</ecf:EntityPerson>
XNamespace ns = "xx";

var doc = XDocument.Load(xmlFilePath);
int idnt =
    int.Parse(
        doc.Descendants(ns + "PersonOtherIdentification")
        .Where(e => e.Element(ns + "IdentificationCategoryText").Value == "IDNT")
        .Single().Element(ns + "IdentificationID").Value);

Console.WriteLine(idnt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM