The XML reader seems to be sensitive about white-space around empty elements.
If I have an empty element with no spaces (<B />
) then the reader doesn't see it as an element.
public static void Main()
{
WriteLine("No spaces around <B>.");
using (var stringReader = new StringReader(@"<Index><A>a</A><B /><C>c</C></Index>"))
using (var reader = XmlReader.Create(stringReader))
{
while (reader.Read())
{
if (reader.Name != "Index" && reader.NodeType == XmlNodeType.Element)
{
WriteLine("{0}: {1}", reader.Name, reader.ReadElementContentAsString());
}
}
}
WriteLine();
WriteLine("Spaces added around <B>.");
using (var stringReader = new StringReader(@"<Index><A>a</A> <B /> <C>c</C></Index>"))
using (var reader = XmlReader.Create(stringReader))
{
while (reader.Read())
{
if (reader.Name != "Index" && reader.NodeType == XmlNodeType.Element)
{
WriteLine("{0}: {1}", reader.Name, reader.ReadElementContentAsString());
}
}
}
Read();
}
Printing out the NodeType
values it looks like it does see it. Here I'm printing our the types found in order (minus the if statement above):
No spaces around <B>. Spaces added around <B>.
Index: Element Index: Element
A: Element A: Element
: Text : Text
A: EndElement A: EndElement
B: Element : Whitespace
C: Element B: Element
: Text : Whitespace
C: EndElement C: Element
Index: EndElement : Text
C: EndElement
Index: EndElement
The problem seems to be with the statement:
reader.ReadElementContentAsString()
If I remove that statement then I get B
appearing again. I thought that it might be something to do with that method moving the reader
to the next node (?) but I can't seem to prove that, or work around it.
How should I handle empty nodes with the XmlReader
?