473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Help with XmlNode - HasChildNodes always returns true even no immediate child exists

RJN
Hi

I have to read an xml and add the node elements into a hashtable with
nodename as key and nodetext as value. If the selected node has
childnodes, then value should go as an array.

for eg.,

<Root>
<mysection>
<logroot>e:\temp</logroot>
<outputdir>
<dir1>e:\temp1</dir1><dir2>e:\temp2</dir2>
</outputdir>
</mysection>
<myanothersection></myanothersection>
</Root>

I would first search for "Root/mysection". Now since logroot doesn't
have further childnodes, I would like to add to the hashtable "logroot"
as key and "e:\temp" as value. The next node outputdir has 2 child nodes
dir1 and dir2. I would like to create a string array for the values of
dir1 and dir2 and add to the hashtable "outputdir" as key and the array
as value.

The following code is not working for me.

string strXml =
"<Root><mysection><logroot>e:\\temp</logroot><outputdir><dir1>e:\\temp1<
/dir1><dir2>e:\\temp2</dir2></outputdir></mysection></Root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strXml);
XmlNode xmlSection = xmlDoc.SelectSingleNode("Root/mysection");
Hashtable htValues = new Hashtable();
foreach(XmlNode node in xmlSection.ChildNodes)
{
if(node.HasChildNodes)
{
string[] str = new string[node.ChildNodes.Count];
for(int i=0;i<node.ChildNodes.Count;i++)
str[i] = node.ChildNodes[i].InnerText;
htValues.Add(node.Name, str);
}
else
htValues.Add(node.Name, node.InnerText);

}
I always get node.HasChildNodes as true even though the node doesn't
have further child nodes. Because of this, I end up adding an array as
value for every node. If the values inside the node is empty then it is
working fine.

Any help is appreciated.

Regards

rjn

*** Sent via Developersdex http://www.developersdex.com ***
Apr 6 '06 #1
1 4924
RJN <rj*@yahoo.com> wrote:
I have to read an xml and add the node elements into a hashtable with
nodename as key and nodetext as value. If the selected node has
childnodes, then value should go as an array.

for eg.,

<Root>
<mysection>
<logroot>e:\temp</logroot>
<outputdir>
<dir1>e:\temp1</dir1><dir2>e:\temp2</dir2>
</outputdir>
</mysection>
<myanothersection></myanothersection>
</Root>

I would first search for "Root/mysection". Now since logroot doesn't
have further childnodes, I would like to add to the hashtable "logroot"
as key and "e:\temp" as value.


logroot *does* have a child node - it has a text node. It doesn't have
any child *elements*, but that's not the same thing.

Hope that helps - if not, I'll try to give some code later on. Time to
put my son to bed first though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 6 '06 #2

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
4
by: Paul M | last post by:
Larry, i've got an xml file which has multiple cities as the top level item. When i read it in using XMLReader, and define the variables, as below, it finds the correct number of cities in the...
4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
2
by: David Elliott | last post by:
I am creating a configuration class to read / write a standard configuration file. <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="ConnectionString"...
1
by: Venkat Venkataramanan | last post by:
Hello: Can somebody explain to me the usage of the XMLNode.ChildNodes? When there is a node that does not have any child node, the above method returns an array of 1. This member has the...
0
by: RJN | last post by:
Hi I have to read an xml and add the node elements into a hashtable with nodename as key and nodetext as value. If the selected node has childnodes, then value should go as an array. for eg.,...
1
by: =?Utf-8?B?QmFydCBTdGV1cg==?= | last post by:
I have the following issue. I'm trying to read to following XML doc. <Main> <Submain> <SubField1/> <SubField2/> </Submain> <Field1/> <Field2>AnyTextValue</Field2> </Main>
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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,...
0
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...

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.