473,395 Members | 1,763 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,395 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 1685
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paweł Gałecki | last post by:
I wrote a following function just for testing purposes: <html> <head> <script> function show(obj) { alert(obj.previousSibling.rowIndex) alert(obj.rowIndex) alert(obj.nextSibling.rowIndex) }...
2
by: Gary | last post by:
I am trying to use the "System.Windows.Forms.SendKeys" class for triggering the Ctrl+P key. Syntax: System.Windows.Forms.SendKeys.Send("^(P)"); This is not working ..what could be the...
6
by: Mullin Yu | last post by:
hi, i have a web service that has file operations on Windows OS, and there may be a file concurrency issue if only one working directory e.g. c:\working therefore, i want to have a unique sub...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
8
by: jojobar | last post by:
Okay, I am trying to do is to test the webresource in 2.0 1. I created a new project with assembly name (and default assembly name) "Office". 2. I added the following to the AssemblyInfo.cs...
2
by: Don | last post by:
I'm having problems with intellisense, autocomplete, etc. suddenly not working in certain classes of a project I'm working on. All the options are set, and it all works fine for most classes, but...
0
by: Asad | last post by:
I am trying to get the PreviousSibling function work. But I keep getting the error (Error accessing XML). Here is my code: ...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.