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] "Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:OVEsYnwTEHA.644@tk2msftngp13.phx.gbl...
>
>
> Pavils Jurjans wrote:
>
>
> > 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
>
> 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/ >