I have the following XML Schema:
<Root>
<EventSet>
<Event>
<id>
//random id
</id>
<time>
<localTime>
//random local time
</localtime>
<utcTime>
//corresponding UTC time
</utcTime>
</time>
</Event>
</EventSet>
</Root>
Given an XDocument (called xDoc in this case), I can get the root by: var root = xDoc.Root;
I tried var events = xDoc.Descendants("EventSet").Descendants("Event"); to query all events within an EventSet, but it returned null. I’m pretty sure that’s not right.
How would I query events, then iterate through to get each event’s id, localTime, and utcTime?
I can’t reproduce your problem. Having fixed the XML so that the tags match up, this works:
Or to get the bits:
It’s possible that you could cast to
DateTimeinstead ofstring– it depends on the format within your element.