473,471 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bug or By Design?

Hi,

I recently had a problem with one of my XPath expressions which led me
to believe there might be a bug in XmlNode, although it could be by
design since I didn't find anything in the KB.

Consider the the code appended below. The function CleanEmptyElements
removes all empty elements from the document using a XPath expression.

As you will notice in the code, I add two empty elements. If you check
the resulting testXml.xml file, the "SpecialEmptyElement" element,
though emtpy is still present.

The only special thing about it is that I assign a null string to the
InnerText property of its node. As far as I know, this shouldn't make a
difference, it should still be an empty element which is confirmed by
the output of the file.

However, if you step through the code with the debugger, you will notice
that as soon as I assign the null string, a new node gets added and the
HasChildNodes property gets set to true.

If you try the same with the InnerXml property, you get the behavior I
was expecting, which is that nothing happens. So there seems to be an
inconsistency here unless I'm mistaken.

So, is this a bug or normal behavior?

Thanks

using System;
using System.Xml;

namespace EmptyElement
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlDocument myDoc = new XmlDocument();

XmlNode currentNode = myDoc.CreateElement("Root");
currentNode = myDoc.AppendChild(currentNode);

XmlNode tempNode = myDoc.CreateElement
("EmptyElement");
currentNode.AppendChild(tempNode);

tempNode = myDoc.CreateElement
("SpecialEmptyElement");
tempNode.InnerText = null;
currentNode.AppendChild(tempNode);

CleanEmptyElements(myDoc);

myDoc.Save("c:\\testXml.xml");
}

static void CleanEmptyElements (XmlDocument myDoc)
{
int i;
XmlNodeList elementsToRemove;

do
{
elementsToRemove = myDoc.SelectNodes("//*[not
(node())]");

for (i = 0; i < elementsToRemove.Count; i++)
{
elementsToRemove
[i].ParentNode.RemoveChild(elementsToRemove[i]);
}
}
while(elementsToRemove.Count > 0);
}
}
}
Nov 12 '05 #1
1 973


Pollux wrote:

I recently had a problem with one of my XPath expressions which led me
to believe there might be a bug in XmlNode, although it could be by
design since I didn't find anything in the KB.

Consider the the code appended below. The function CleanEmptyElements
removes all empty elements from the document using a XPath expression.

As you will notice in the code, I add two empty elements. If you check
the resulting testXml.xml file, the "SpecialEmptyElement" element,
though emtpy is still present.

The only special thing about it is that I assign a null string to the
InnerText property of its node. As far as I know, this shouldn't make a
difference, it should still be an empty element which is confirmed by
the output of the file.

However, if you step through the code with the debugger, you will notice
that as soon as I assign the null string, a new node gets added and the
HasChildNodes property gets set to true.


If you set InnerText to null the element gets one text child node which
is empty. I don't know whether that is intended but that seems to be
what happens.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: adb | last post by:
I came up with a replication configuration that is basically the result of all the restrictions of replication as well as the restrictions of allowable software on work PC's and I was curious if...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
0
by: Edward Diener | last post by:
In Borland's VCL it was possible to divide a component into design time and run time DLLs. The design time DLL would only be necessary when the programmer was setting a component's properties or...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Paul Cheetham | last post by:
Hi, I have moved an application from VS2003 to VS2005, and I am now unable to view most of my forms in the designer. The majority of the forms in my project are derived from class PACForm,...
1
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
4
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.