Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

Incorrect node depth with XmlTextReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Agent Smith - 26 Oct 2003 02:05 GMT
I'm trying to follow the sample code provided in KB
330597. Incidentally, this is exactly the problem I'm
trying to solve as described in the KB article.

In short, if the input XML has a value in every node
everything works great. But... here's what happens when a
node has no value (<node />):

Input XML:
<book>
   <comments />
   <isbn>1234-x</isbn>
   <title>Some title</title>
</book>

Output XML:
<?xml version="1.0" encoding="utf-16"?>
<book>
   <comments>
      <isbn>1234-x</isbn>
      <title>Some title</title>
   </comments>
</book>

Anybody notices that the <comments> tag now wraps
everything??? Indeed, when debugging you can see that the
Depth of the <comments> node is 2 for some reason (!!).

My question is: is there a workaround? Why does
XmlTextReader goof up on an empty node?

Thank you for any input in advance!
Agent Smith
Kirk Allen Evans [MVP] - 27 Oct 2003 02:19 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
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.