Groups | Blog | Home
all groups > dotnet xml > june 2004 >

dotnet xml : Distinguishing empty elements


Martin Honnen
6/10/2004 5:27:47 PM


[quoted text, click to view]


[quoted text, click to view]

Well MSIE expects HTML pages (content type text/html) and parses them
with a HTML parser, and in HTML the <script> element is supposed to have
a closing tag (end tag):
http://www.w3.org/TR/html4/interact/scripts.html#edef-SCRIPT
so you shouldn't take that as a criteria to judge the difference between
<script></script> and <script/> in XML.
I will have a look at the .NET DOM differences you have cited.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Pavils Jurjans
6/10/2004 5:34:38 PM
Hello,

Here's the sample XML:

<sample1></sample1>

<sample2/>

From many XML books and online documentations, it is said to be just
different syntax for the same data. However, when we analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false

As it seems, .NET built-in classes use this difference, when the node is
asked for its InnerXml, as when I output the InnerXml for this samples
document element node (it contains both kinds of "empty" tags), it renders
them differently.

So, what's actually the status of this "considered the same", but actually
"well, not *exactly* the same" two syntaxes of empty nodes? I stumbled upon
this difference, when I got <script src="abc.js"></script> rendered as
<script src="abc.js"/> (using different template solution than ASPX), and
MSIE chuckled on that one.

Rgds,

Pavils

Martin Honnen
6/10/2004 6:41:52 PM


[quoted text, click to view]


[quoted text, click to view]

I dont' get that difference here with .NET 1.1, example XML document being

<?xml version="1.0" encoding="UTF-8"?>
<root>
<empty1/>
<empty2></empty2>
</root>

test program in C# being

using System;
using System.Xml;

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}: element.InnerXml == null: {1},"
+ " element.InnerXml == \"\": {2}.",
element.Name,
element.InnerXml == null,
element.InnerXml == ""
);
}
}
}

I get the output

Element name: empty1: element.InnerXml == null: False, element.InnerXml
== "": True.
Element name: empty2: element.InnerXml == null: False, element.InnerXml
== "": True.

so here with that test InnerXml is the empty string for both element nodes.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Pavils Jurjans
6/11/2004 12:56:21 PM
Yeah, your'e right... there was some glitch in my code... InnerXml is never
null..

However, there is a way how to distinguish the two "empty" elements:

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
Console.Read();
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}; Has no child nodes: {1}; Has closing tag:
{2}",
element.Name,
!element.HasChildNodes,
!element.IsEmpty
);
}
}
}

I get this output:

Element name: empty1; Has no child nodes: True; Has closing tag: False
Element name: empty2; Has no child nodes: True; Has closing tag: True


So, there *is* a way to check for truly empty elements. I find it good,
because it gives a way to communicate "null" value for string variables
whose "zero-length string" value is meaningful, and should be treated
differently than "no value".

Rgds,

Pavils



[quoted text, click to view]

AddThis Social Bookmark Button