473,320 Members | 1,828 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,320 software developers and data experts.

Find "parent" node of element using XmlTextReader

Hi... I am working on a simple program (in VB.net) that will grab the
values of elements out of an XML file that are tagged as
"databasePath"... My program works, but I am getting too many
results. I need to limit the scope of the program to only include the
string value from the element that has a parent of "numberone", and
not both "numberone" and "numbertwo". I don't know if "numberone" is
a node, or what the correct term for it is - I am not too well-versed
in XML. :) I'm getting two lines of output in the console - both
databasePath strings from both the numberone section and the numbertwo
section. Is there any way I can limit the results to just the
numberone section, or numbertwo section by themselves?
My code is as follows, and the XML example is below that. Any help
would be greatly appreciated! Thanks!
VB Code:
Private Sub GetProgs()

Dim objReader As New XmlTextReader(strDASPath)

While objReader.Read()
If objReader.MoveToContent() = XmlNodeType.Element And
objReader.Name = "databasePath" Then

Console.WriteLine(objReader.ReadString())

End If
End While

End Sub

XML Example:

<?xml version="1.0"?>
<!DOCTYPE dasconfig SYSTEM "dasconfig.dtd" >
<dasconfig>

<global>
<annotation>
<advancedUser>true</advancedUser>
</annotation>
</global>
<dataset name="SampleData">
<numberone>
<database>
<Access>
<databasePath>C:\dvlp\test\numberone.mdb</
databasePath>
</Access>
</database>
</numberone>
<numbertwo>
<database>
<Access>
<databasePath>C:\dvlp\test\numbertwo.mdb</databasePath>
</Access>
</database>
</numbertwo>
</dataset>
</dasconfig>

Sep 28 '07 #1
2 10573
I have figured out a way to make it skip the "numbertwo" items, by
placing the following if then statement BEHIND my original if then
statement:

If objReader.Name = "numbertwo" Then
objReader.Skip()
End If

However, I'm still a little confused. Why does this not work when I
place the same statement ABOVE the original if then statement?
Shouldn't it skip the children of numbertwo regardless of where it
encounters them in the loop?

Sep 28 '07 #2
Ok - I figured that out, too... Looks like the MoveToContent() method
is the key here - without it, the reader won't move to different lines
in the xml file. My new code is posted below, and works flawlessly:

While objReader.Read()

objReader.MoveToContent()

If objReader.Name = "numbertwo" Then
objReader.Skip() 'Skip all children
End If

If objReader.Name = "databasePath" Then

Console.WriteLine(objReader.ReadString())

End If

End While

Any insight from a more knowledgable person would be appreciated...
Just in case my understanding is flawed. Thanks!!!

Sep 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: balg | last post by:
An ActiveX control has a call to "UserControl.Parent" which fails when it is interopped and added to a .Net Form. Is there any way to access to .Net parent control/it's interfaces from VB6...
4
by: nicver | last post by:
I am fixing a client's Web site and I do not have the time to reprogram things my way, and the last hurdle I have is with a piece of javascript embedded in an ASP snippet. The payment form is...
3
by: Brian Henry | last post by:
If i already have a tree view created, and want to add another new node to it, how would i do so? Is there a way to throught tags or anything? like i have this RootNode | +-- Child 1 +--...
14
by: neerajb | last post by:
Hi, I am having an XML document(input.xml) which is showing the menu heirarchy used in my application.My requirement is to add "submenu" tag to those menuitems who are having the child menuitems...
6
by: Ray Schumacher | last post by:
What is the feeling on using "parent" in a class definition that class methods can refer to, vs. some other organization ? Should all relevant objects/vars just be passed into the method as needed?...
2
by: dd | last post by:
I need to know whether an element can move itself to a new parent within the DOM. I can't find any function that offers me such an ability. Here's an example of a hierarchy: -body ....div 1 ...
2
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I want to give a user the possibility of "restarting" an interactive session, by removing all the objects defined by her since the beginning. The way I make this possible is by having a "function"...
2
by: Rob Stevens | last post by:
When restoring nodes to a tree, how can you tell what is the parent of that node? Also how can you get a handle to the parent node, this way you can insert the node into the correct place? ...
3
by: eBob.com | last post by:
How does a "sub-form", i.e. one invoked by another form, determine anything about the form which brought it into existence, i.e., I suppose, instantiated it? I wanted to so something like this ......
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...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.