XML Question

H

Hardy Wang

Guys,
I have following piece of XML file,
<?xml version="1.0"?>
<SiteSettings>
<PageSettings>
<Image1 source="dlr" type="jpg">
<url source="dlr">http://www.hotmail.com</url>
<newwindow source="dlr">yes</newwindow>
</Image1>
</custom_awards>
</PageSettings>
</SiteSettings>

I have following code to load this XML file
string xmlKey = "SiteSettings/PageSettings/custom_awards/Image1/url";
XmlDocument dlrProductionXDoc = new XmlDocument();
dlrProductionXDoc.Load(@"c:\temp\test.xml");
XmlNode xNode1 = dlrProductionXDoc.SelectSingleNode(xmlKey);

I read the HasChildNodes of this "url" node, but the value is true, even
though there is no more nodes under it. From quick watch, its LastChild's
name is "#text" .
How can I detect if a node is the last level of nodes (no more children)?
 
K

Kai Brinkmann [MSFT]

Hello Hardy,

You're seeing this result because the text inside an XML element node is
still considered a child of the node (a text node rather than an element
node). You need to also check the XmlNodeType property. Thus, the last
element node is a node with at most one child whose node type is Text. You
could check this in the following manner:

if (!xNode1.HasChildNodes ||
(xNode1.ChildNodes.Count == 1 && xNode1.FirstChild.NodeType ==
XmlNodeType.Text))
{}

The first condition simply checks whether there are children. If not, you
have found a terminal node and the second condition isn't even checked. The
second condition checks whether there is just one child, and if so makes
sure that the child is a text element. If that is the case, you have found a
terminal node containing inner text.

In either case you have found a terminal node containing no child elements.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hardy Wang

Thanks a lot!!

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
Kai Brinkmann said:
Hello Hardy,

You're seeing this result because the text inside an XML element node is
still considered a child of the node (a text node rather than an element
node). You need to also check the XmlNodeType property. Thus, the last
element node is a node with at most one child whose node type is Text. You
could check this in the following manner:

if (!xNode1.HasChildNodes ||
(xNode1.ChildNodes.Count == 1 && xNode1.FirstChild.NodeType ==
XmlNodeType.Text))
{}

The first condition simply checks whether there are children. If not, you
have found a terminal node and the second condition isn't even checked.
The
second condition checks whether there is just one child, and if so makes
sure that the child is a text element. If that is the case, you have found
a
terminal node containing inner text.

In either case you have found a terminal node containing no child
elements.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no
rights.


Hardy Wang said:
Guys,
I have following piece of XML file,
<?xml version="1.0"?>
<SiteSettings>
<PageSettings>
<Image1 source="dlr" type="jpg">
<url source="dlr">http://www.hotmail.com</url>
<newwindow source="dlr">yes</newwindow>
</Image1>
</custom_awards>
</PageSettings>
</SiteSettings>

I have following code to load this XML file
string xmlKey = "SiteSettings/PageSettings/custom_awards/Image1/url";
XmlDocument dlrProductionXDoc = new XmlDocument();
dlrProductionXDoc.Load(@"c:\temp\test.xml");
XmlNode xNode1 = dlrProductionXDoc.SelectSingleNode(xmlKey);

I read the HasChildNodes of this "url" node, but the value is true, even
though there is no more nodes under it. From quick watch, its LastChild's
name is "#text" .
How can I detect if a node is the last level of nodes (no more children)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top