0

I've run into an issue with parsing XML in .Net where I need to be able to detect which form of empty element I have but can't seem to get this to work correctly. Essentially in the XML format I'm parsing the following two fragments should parse differently:

<sometag />

and

<sometag></sometag>

My problem is that .Net does not appear to provide me with any means to determine the different between the above.

Using DOM based parsing the XmlNode will report '""' for both the InnerText and InnerXml and the OuterXml property expands to the second form regardless of the input XML so no way to detect based on that.

Using XmlReader based parsing both forms report IsEmptyElement to be true and I can't see any other properties of any use to detect this.

Anyone know of any way to detect this for DOM based parsing

1 Answer 1

1

In the first case IsEmptyElement returns true when you are at the start element and in the second case it returns false:

while (reader.Read())
{
    if (reader.NodeType == XmlNodeType.Element && reader.Name == "sometag")
    {
        // prints true if <sometag/> and false if <sometag></sometag>
        Console.WriteLine(reader.IsEmptyElement);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Corrected my question, you are right that this does report appropriately for XmlReader based parsing, would still be nice to be able to detect the same for DOM based parsing
Pardon my ignorance but what is DOM based parsing?
Document Object Model = DOM . If you are using the XmlDocument class then .Net will turn the entire XML document into a tree of objects which depending on your XML format can make parsing easier since you can move freely through the data rather than forward only. The downside is that it requires far more memory than doing XmlReader parsing especially if you have large XML files

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.