all groups > dotnet xml > april 2004 >
You're in the

dotnet xml

group:

PreviousSibling not working


PreviousSibling not working asadikhan NO[at]SPAM hotmail.com
4/28/2004 1:49:01 PM
dotnet xml:
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?

Re: PreviousSibling not working Derek Harmon
4/28/2004 7:37:29 PM
[quoted text, click to view]

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.

: :
[quoted text, click to view]
: :

This line needs to be,

node_temp = node.ParentNode.PreviousSibling.FirstChild

and here's why,

: :
[quoted text, click to view]

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

AddThis Social Bookmark Button