473,241 Members | 1,517 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,241 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 1682
"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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.