Getting an xml node that appears more than once linq c#

  • Thread starter Thread starter CuriousCoder15
  • Start date Start date
C

CuriousCoder15

Guest
Hi,

I am reading in xml data to a winform using XDocument,

my xml looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE>
<Events time="20190205T150013+0100">
<Event update="2" date="20190205" id="105">
<EventDetails date="20190205" id="105" participants="6"
time="1500+0100">
<Weather>Rain</Weather>
<Event status="On" id="2075" name="Davidsons">
<Number ="1"/>
<Weight ="9st"/>
<Section timestamp="20190205T1450+0100">
<Cost high="250" low="125"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="220" low="105"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="200" low="100"/>
</Section>
</Event>
<Event status="On" id="2075" name="Davidsons">
<Number ="1"/>
<Weight ="9st"/>
<Section timestamp="20190205T1450+0100">
<Cost high="250" low="125"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="220" low="105"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="200" low="100"/>
</Section>
</Event>
<Event status="On" id="2075" name="Davidsons">
<Number ="1"/>
<Weight ="9st"/>
<Section timestamp="20190205T1450+0100">
<Cost high="250" low="125"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="220" low="105"/>
</Section>
<Section timestamp="20190205T1450+0100">
<Cost high="200" low="100"/>
</Section>
</Event>



Under the <Event> node there are multiple <Section> nodes, I want to get the last one only of each Event section, how can I do this?

I am going by what I have read in other posts in the below code, I want to get the date, id and participants of <EventDetails>

then get the very last <Section timestamp and <Cost high="" low="" of each <Event>

var eventElements = doc.XPathSelectElements("Events");
var items = from e in eventElements
select new
{
date = e.XPathSelectElement("Events/Event/date")?.Value,
id = e.XPathSelectElement("Events/Event/id")?.Value
}


Any help would be really appreciated.





CuriousCoder

Continue reading...
 
Back
Top