471,610 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

PreviousSibling not working

I am trying to get the PreviousSibling function work. But I keep
getting the error (Error accessing XML). Here is my code:

---------------------------------------------------------------------------

private i as integer
private strOutput as string = ""

sub page_load (obj as object, e as eventargs)

dim xmldoc as new XMLDocument()

try
xmldoc.Load(Server.MapPath("Geo.xml"))
ShowTree(xmldoc.DocumentElement)

catch ex as Exception
strOutput = "Error accessing XML file"
end try

output.Text = strOutput

end sub

sub ShowTree(node as XMLNode)

dim attrnode as XmlNode
dim map as XmlNamedNodeMap

If Not(node.HasChildNodes)
strOutput += "&nbsp&nbsp;<b>" & node.Value & "</b><br/>" &
vbcrlf

if (node.Value = "Alabama") then
dim node_temp as XMLNode
node_temp = node.PreviousSibling
strOutput += "&nbsp&nbsp;<b>" & node_temp.Value &
"</b><br/>" & vbcrlf
end if

Else

strOutput += "<b>" & node.Value & "</b>"

If node.HasChildNodes then
node = node.FirstChild
while not IsNothing(node)
ShowTree(node)
node = node.NextSibling
end while
end if

end if
end sub

---------------------------------------------------------------------------

The line that causes the error is the last one in this if statement:

if (node.Value = "Alabama") then
dim node_temp as XMLNode
node_temp = node.PreviousSibling
strOutput += "&nbsp&nbsp;<b>" & node_temp.Value &
"</b><br/>" & vbcrlf
end if

If I comment out the last line, everything works fine. My XML looks
like this:

<?xml version="1.0" encoding="UTF-8" ?>
- <Geo>
<abbrev>AL</abbrev>
<state>Alabama</state>
<capital>Montgomery</capital>
<timezone>C</timezone>
</Geo>
- <Geo>
<abbrev>AK</abbrev>
<state>Alaska</state>
<capital>Juneau</capital>
<timezone>A</timezone>
</Geo>
....

Any idea what I am doing wrong?

Thanks.
Nov 12 '05 #1
1 1630
"Asad" <as*******@hotmail.com> wrote in message news:fd**************************@posting.google.c om...
I am trying to get the PreviousSibling function work. But I keep
getting the error (Error accessing XML).
That error message doesn't help troubleshooting, does it?

If you look at the real Exception, NullReferenceException,
you'll get much more valuable information indicating that
node_temp is Nothing.

: : node_temp = node.PreviousSibling : :

This line needs to be,

node_temp = node.ParentNode.PreviousSibling.FirstChild

and here's why,

: : - <Geo>
<abbrev>AL</abbrev>
<state>Alabama</state>


The node tree for this markup really looks like this,

Geo
|
|\_abbrev
| \
| \_ # text (AL)
|
\_ state
\
\_ # text (Alabama)

From this diagram, you see that temp_node is really an XmlNode with
XmlNodeType == XmlNodeType.Text. These are the nodes that have
the text values: "AL" and "Alabama."

When you ask for the PreviousSibling of the #text node, you receive
Nothing because these elements have only one [normalized] text node.
It's an orphan, it has no siblings.

Therefore, what I suspect you want is Alabama's parent (the XmlNode
of XmlNodeType == XmlNodeType.Element) named "state", then you
want state's previous sibling (or uncle), which is the element named
"abbrev." Finally, descending to take the first child (Alabama's cousin)
of "abbrev" to get at "AL".

Remember that XmlElement nodes can have XmlText node children,
and it was these orphaned text nodes that you're processing.
Derek Harmon
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paweł Gałecki | last post: by
2 posts views Thread by Gary | last post: by
6 posts views Thread by Mullin Yu | last post: by
5 posts views Thread by Martin Heuckeroth | last post: by
5 posts views Thread by tshad | last post: by
8 posts views Thread by jojobar | last post: by
2 posts views Thread by Don | last post: by
reply views Thread by Asad | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.