Question

Say that I have this xml:

<Parent>
    <Child>
        <ChildOfChild>
        </ChildOfChild>
    </Child>
    <Child2>
    </Child2>
</Parent>

I would like to get the name of the nodes?, I know I can get the name of the parent with:

XDocument xd;
String test;
test = xd.Root.Name.LocalName; // test would contain "Parent"

But how do I get the name of the children?

As I can get the next child with xd.Root.FirstNode but I cant find any property or similar to get the name of the child, in this case Child

Était-ce utile?

La solution

xd.Root.FirstNode returns the first child node of the root element, which is not necessary an element (could be a comment for example, which don't have names).

Try the following:

xd.Root.Elements().First().Name.LocalName;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top