The KB article does not check for XmlReader.IsEmptyElement [1]. The node
type is XmlNodeType.Element, so it will write a start element. Empty
elements do not have an end element, so the nesting fails. At the end of
the loop, the article shows xmlWriter.Close(), which pops the stack of all
open elements and closes them. The article text works because there are no
empty elements in their sample.
[1]
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemXmlXmlReaderClassI
sEmptyElementTopic.asp?frame=true

Signature
Kirk Allen Evans
Microsoft MVP, ASP.NET
XmlInsiders
www.xmlandasp.net
Read my web log at http://weblogs.asp.net/kaevans
> I'm trying to follow the sample code provided in KB
> 330597. Incidentally, this is exactly the problem I'm
[quoted text clipped - 29 lines]
> Thank you for any input in advance!
> Agent Smith
Agent Smith - 27 Oct 2003 17:04 GMT
>The KB article does not check for XmlReader.IsEmptyElement [1]. The node
>type is XmlNodeType.Element, so it will write a start element. Empty
[quoted text clipped - 5 lines]
>[1]
>http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemXmlXmlReaderClassI
sEmptyElementTopic.asp?frame=true
Hi Kirk,
IsEmptyElement works. The MSDN page you are referring to has this to
say:
"A corresponding EndElement node is not generated for empty elements."
Well, it *is* generated except that it comes at the wrong time. In the
code snip I provided see where the closing </comments> tag comes---too
late in the game. I was expecting it to appear right afther the
opening tag at least.
I even tried to force a closing tag via xmlWriter.WriteEndElement ()
but... the reader still reads the </comments> closing tag (case
XmlNodeType.EndElement) which screws everything up.
I'm lost. I'd think it's a trivial matter to properly open and close
and empty tag or at least NOT trigger the closing tag. How many XML
documents are full of data at all times? This is a rhetorical question
but still... what's the real-life fix?
Agent Smith
Kirk Allen Evans [MVP] - 28 Oct 2003 11:23 GMT
while(xmlReader.Read())
{
switch(xmlReader.NodeType)
{
case XmlNodeType.Element:
xmlWriter.WriteStartElement(xmlReader.Name);
elementName = xmlReader.Name;
///Added the following code
if(true == xmlReader.IsEmptyElement)
xmlWriter.WriteEndElement(xmlReader.Name);
break;
case XmlNodeType.Text:
if(elementName.ToLower() == "birthdate")
xmlWriter.WriteString(XmlConvert.ToDateTime(xmlReader.Value).ToString());
else
xmlWriter.WriteString(xmlReader.Value);
break;
case XmlNodeType.EndElement:
xmlWriter.WriteEndElement();
break;
}
}
xmlWriter.Close();

Signature
Kirk Allen Evans
Microsoft MVP, ASP.NET
XmlInsider
www.xmlandasp.net
Read my web log at http://weblogs.asp.net/kaevans
> >The KB article does not check for XmlReader.IsEmptyElement [1]. The node
> >type is XmlNodeType.Element, so it will write a start element. Empty
[quoted text clipped - 29 lines]
>
> Agent Smith
Agent Smith - 31 Oct 2003 04:11 GMT
>while(xmlReader.Read())
>{
[quoted text clipped - 7 lines]
> if(true == xmlReader.IsEmptyElement)
> xmlWriter.WriteEndElement(xmlReader.Name);
[skipped]
I've tried this before I posted my question. Unfortunately, still no
go. Here's what happens (refer to the "Output XML" from the original
post):
<?xml version="1.0" encoding="utf-16"?>
<book>
<comments/> <-- It will close the tag
<isbn>1234-x</isbn>
<title>Some title</title>
</book> <-- the "close tag" is still read even though you've
already closed it manually!!!
<-- an excepiton is thrown here
Agent Smith