Problem with XmlReader.IsEmptyElement property

I was doing some basic XML processing with XmlReader class (in VisualStudio 2005 Beta 2). The code was supposed to fire events when it encounters either start or end of an XML element. According to .Net documentation the XmlReader does not explicitly read XmlNodeType.EndElement for empty elements (i.e. ones ending with />) - we are supposed to check the value of IsEmptyElement property to detect the end of empty XML elements.
This indeed works for empty elements with NO ATTRIBUTES such as <myelement/>.
But it does not seem to work for empty elements WITH attributes. For instance for this element:
<myelement attr1="value"/>
the XmlReader does not read the end of element (as expected) but IsEmptyElement property is false as if it's a regular element! So is it a bug? How are we supposed to detect the end of such elements?
The following is the skeleleton of the test code (in J#):
XmlReader = ... // get the reader
while (reader.Read())
{
XmlNodeType nodeType = reader.get_NodeType();
switch (nodeType)
{
case XmlNodeType.Element:
// fire StartElement event
....
// special case: if we encounter an empty element (such as <element/>)
// then the XMLReader will not read the closing tag for it,
// i.e. XmlNodeType.EndElement will not work
// instead we need to check explicitly if the element is empty and if it is
// then fire endElement SAX event
if (reader.get_IsEmptyElement())
{
// fire EndElement event
}
break;
case XmlNodeType.EndElement:
// fire EndElement event
break;
// other cases
}
[1673 byte] By [DmitriyKot] at [2008-2-11]
# 1

I can't reproduce the problem. If I run this:

string
xml = @"<foo><myelement attr1=""value""/></foo>";
XmlReader reader = XmlReader.Create(new StringReader(xml));
while (reader.Read())
{
Console.WriteLine("NodeType: {0}, IsEmptyElement: {1}", reader.NodeType, reader.IsEmptyElement);
}

I get as expected:
NodeType: Element, IsEmptyElement: False
NodeType: Element, IsEmptyElement: True
NodeType: EndElement, IsEmptyElement: False
Provide some repro please.

OlegTkachenko at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2
If reader points to attribute IsEmptyElement will return false. Make sure you are calling MoveToElement() after all attribute reading operations and before IsEmptyElement.
TheXMLMan at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3
Yep, that was exactly the problem - there was a call to MoveToAttribute before the call IsEmptyElement. Somehow I overlooked it. Aah, the power of assumption :-))
Anyways, guys, thanks for your help. I really appreciate it!
DmitriyKot at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified