Search code examples
.netxmlxmlreader

Detect two forms of empty elements in XML using .Net


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


Solution

  • 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);
        }
    }