0

How to differenciate between the two nodes

<Header Name="ABC" />
and
<Test Test="AA">
Hello
</Test>

using XmlReader? The problem is that I am not been able to know whether a node contains child or not using XmlReader.

1 Answer 1

1

See MSDN: XmlReader.Read Method - "When overridden in a derived class, reads the next node from the stream."

There is an example on that MSDN page, however I think you want to do something like this:

using(var reader = XmlReader.Create(stream))
{
    while(!reader.EOF)
    {
        reader.Read();

        if(reader.IsEmptyElement)
        {
            ...
        }
    }
}

The trick is when you understand that each time you go round the while loop and call reader.Read(); you advance to the next node, so when you call any other methods/properties on the reader, they will act on whatever the current node is.

As an alternative you could use XPath and check the XmlNode.HasChildNodes property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.