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

有帮助吗?

解决方案

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top