Sign In/My Account | View Cart  
advertisement

Article:
 Learning C# XML
Subject: Descrepancies between Java and C#
Date: 2002-03-21 08:42:44
From: Dennis Wilson

I tried a much simpler experiment based on your code and I ran into a remarkable descrepancy.


It occurs for tags that have only attributes or are placeholders like <ttt/>


Using both Java and C#, I simply attempted to increment and decrement a level counter when entering startElement and leaving endElement.


The numbering of the levels remained consistent with Java and both the start and end callbacks fired as expected. When using the XmlTextReader from C# I was forced to rely on the XmlNodeType.Element and XmlNodeType.EndElement values produced by the call to Read(). What occurred surprised me.


It seems that tags like <ttt/> do not fire an EndElement change. If the tag was changed to <ttt></ttt> then the numbering match the outcome from using SAX with Java.


example: from C#
1 1
<b/> 2 2
2
2 1
If you might be interested I can email the code and example xml files.


Your article was very interesting and clearly written. What I got from this is that there are some very subtle "features" awaiting the person who attempts to translate code between Java and C#.


Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • Descrepancies between Java and C#
    2002-03-21 10:52:01 Niel Bornstein [Reply]

    Dennis -


    You raise a very interesting point. I did a little research and discovered the XmlReader.IsEmptyElement property.


    The Microsoft documentation (http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemXmlXmlTextReaderClassIsEmptyElementTopic.asp) reports that IsEmptyElement is "true if the current node is an element (NodeType equals XmlNodeType.Element) that ends with />; otherwise, false." And further, "A corresponding EndElement node is not generated for empty elements."


    I have not tested this myself, but it seems that one could check for this case by inserting the following code at the very end of the XmlNodeType.Element case between the call to StartElement() and the break statement:


    if (reader.IsEmptyElement) {
    EndElement(reader.NamespaceURI, reader.Name, reader.Name);
    }


    The intent is to immediately fire off a call to EndElement() when the element is empty, since the framework does not do it automatically.


    Niel


Sponsored By: