Frage

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

War es hilfreich?

Lösung

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;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top