I have a simple issue but I don't know how to fix it. I have an XML document that looks like this
<hotel>
<rooms>
</rooms>
<rates>
<rooms>
</rooms>
</rates>
</hotel>
Now, in my code I have the following
XElement hotel = xDoc.Descendants("hotel").Single();
XElement rooms = hotel.Descendants("rooms").Single();
The last line fails because there are two rooms
nodes. What I want Descendants
to do is give the immediate descendants of the current node, not every descendant no matter where it is in the document. How is this possible?
Thanks,
Sachin
To get the first rooms
child element of the hotel
node, use the Element
method:
XElement rooms = hotel.Element("rooms");
Also, if you're really using an XDocument
then the hotel
node would be the root, and can be accessed using the xDoc.Root
property.