I have a complex xml and I need to get all the elements
<sdnEntry>
containing the value "Individual" in the tag <sdnType>
this is my XML:
string list = @"<?xml version='1.0' standalone='yes'?>
<sdnList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://tempuri.org/sdnList.xsd'>
<sdnEntry>
<uid>36</uid>
<lastName>AEROCARIBBEAN</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>CUBA</program>
</programList>
</sdnEntry>
<sdnEntry>
<uid>173</uid>
<lastName>ANGLO-CARIBBEAN</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>CUBA</program>
</programList>
</sdnEntry>
<sdnEntry>
<uid>2681</uid>>
<title>NAME1 SURNNAME1</title>
<sdnType>Individual</sdnType>
<programList>
<program>SDGT</program>
</programList>
</sdnEntry>
<sdnEntry>
<uid>2682</uid>
<title>NAME2 SURNNAME2</title>
<sdnType>Individual</sdnType>
<programList>
<program>SDGT</program>
</programList>
<idList>
<id>
<uid>1002</uid>
<idType>Passport</idType>
<idNumber>304555</idNumber>
<idCountry>Egypt</idCountry>
</id>
</idList>
</sdnEntry>
</sdnList>";
I have tried this so far, but it is not working:
listXML.LoadXml(list);
XDocument sndList = XDocument.Parse(listXML.OuterXml);
var nls = XNamespace.Get("http://tempuri.org/sdnList.xsd");
//TEST 1
//--------------------------------------------------------------
var individ = sndList.Elements(nls "sdnEntry")
.SelectMany(r => r.Descendants("sdnType").Where(e => (string)e.Element("sdnType").Value == "Individual"));
//TEST 2
//--------------------------------------------------------------
IEnumerable<XElement> individuals = from element in sndList.Root.Elements(nls "sdnEntry")
where (string)element.Element("sdnType") == "Individual"
select element;
what am I doing wrong?
Thanks for helping.
CodePudding user response:
Use this:
var individualNodes = sndList.Root.Elements(nls "sdnEntry")
.Where(e => e.Element(nls "sdnType").Value == "Individual");