2011-08-08 99 views
1

下面给出的XML ..简单的LINQ to XML问题

,并考虑到我有两个变量“IDNT”和“外部参照”将存储#ID ..我如何获得这些价值?

我想

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> 

回答

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