473,396 Members | 2,113 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,396 software developers and data experts.

Reading XML File

Ian
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " "
& ex.Source)
End Try
Nov 21 '05 #1
6 1647
What are you actually trying to display?

There is no "SERVER" node in your XML snippet. Even if you change to access
"SERVERS" there is no InnerText on any of those entries. (Just attributes).
Also, why don't you use the .NET Framework instead of the COM XML DOM?

"Ian" wrote:
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " "
& ex.Source)
End Try

Nov 21 '05 #2
Ian
Thanks for the quick response.

Right now I'm just trying to display the server HOSTNAMES and DATABASE names
(baby steps). Can you give me a hint to how I use the .NET framework to do
this?

Thanks
"TrtnJohn" wrote:
What are you actually trying to display?

There is no "SERVER" node in your XML snippet. Even if you change to access
"SERVERS" there is no InnerText on any of those entries. (Just attributes).
Also, why don't you use the .NET Framework instead of the COM XML DOM?

"Ian" wrote:
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " "
& ex.Source)
End Try

Nov 21 '05 #3
Look in the Visual Studio help file for the class System.Xml.XmlTextReader.
The help file gives examples of how to use each method in the documentation
of the method. Some methods you want to look at are: Read and GetAttribute.

"Ian" wrote:
Thanks for the quick response.

Right now I'm just trying to display the server HOSTNAMES and DATABASE names
(baby steps). Can you give me a hint to how I use the .NET framework to do
this?

Thanks
"TrtnJohn" wrote:
What are you actually trying to display?

There is no "SERVER" node in your XML snippet. Even if you change to access
"SERVERS" there is no InnerText on any of those entries. (Just attributes).
Also, why don't you use the .NET Framework instead of the COM XML DOM?

"Ian" wrote:
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " "
& ex.Source)
End Try

Nov 21 '05 #4
Hi,
what you need is a "namespace manager" to deal with the namespace that is in
your xml doc. If you add the following code:

Dim xreader as XmlTextReader = new XmlTextReader("../config.xml")
Dim xns XmlNamespaceManager = new new XmlNamespaceManager(xreader.NameTable)
xns.AddNamespace("abc", "http://tempuri.org/config1.xsd")

...... and a little modification to your xpath query:
xml_nodelist = xml_doc.SelectNodes("abc:CAREKEEPER",xns)
gets the node successfully.

Note: I sort of hard coded the "abc" and the uri, I think u could write it
in a generic way. Also the xpath is case-sensitive, so be careful while
writing node names in xpath queries.

Second solution:
Just remove the xmlns attribute from the CAREKEEPER node in your xml file
and the code you write should work without the "/" before CAREKEEPER in
xpath query.

I hope this helps.

Ab.
http://joehacker.blogspot.com

"Ian" <Ia*@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " " & ex.Source)
End Try

Nov 21 '05 #5
and to access attribute you have to write :
xml_node.Attributes["hostname"];

Any xml related classes I used r inside System.Xml;

Ab.
http://joehacker.blogspot.com

"Abubakar" <ab*******@gmail.com> wrote in message
news:Ot**************@TK2MSFTNGP10.phx.gbl...
Hi,
what you need is a "namespace manager" to deal with the namespace that is in your xml doc. If you add the following code:

Dim xreader as XmlTextReader = new XmlTextReader("../config.xml")
Dim xns XmlNamespaceManager = new new XmlNamespaceManager(xreader.NameTable) xns.AddNamespace("abc", "http://tempuri.org/config1.xsd")

..... and a little modification to your xpath query:
xml_nodelist = xml_doc.SelectNodes("abc:CAREKEEPER",xns)
gets the node successfully.

Note: I sort of hard coded the "abc" and the uri, I think u could write it
in a generic way. Also the xpath is case-sensitive, so be careful while
writing node names in xpath queries.

Second solution:
Just remove the xmlns attribute from the CAREKEEPER node in your xml file
and the code you write should work without the "/" before CAREKEEPER in
xpath query.

I hope this helps.

Ab.
http://joehacker.blogspot.com

"Ian" <Ia*@discussions.microsoft.com> wrote in message
news:7C**********************************@microsof t.com...
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message &

" "
& ex.Source)
End Try


Nov 21 '05 #6
Hi,

did you try:

xml_nodelist = xml_doc.SelectNodes("/CAREKEEPER")

Perhaps it is a case sensitive issue?

Markus

Ian wrote:
Hi

I'm pretty new at this so please don't laugh too hard. I'm trying to load
an xml document using VB.NET and having a hard time. My code doesn't crash
but it doesn't work either, the first msgbox returns 0.

Help (please!)
Below is my document and snippet of code

<?xml version="1.0" encoding="utf-8"?>
<CAREKEEPER xmlns="http://tempuri.org/config1.xsd">
<TABLEINFORMATION TABLE="workers" ssn="ssn" emplid="userinput1">
</TABLEINFORMATION>
<SERVERS hostname="sta0wp02" database="vividcare">
</SERVERS>
<SERVERS hostname="rdy1cn02" database="vividcare">
</SERVERS>
</CAREKEEPER>

CODE:

Try
xml_doc.Load("../config.xml")
xml_nodelist = xml_doc.SelectNodes("/carekeeper")

MsgBox(xml_nodelist.Count())

For Each xml_node In xml_nodelist
server = xml_node.Item("SERVER").InnerText()
MsgBox(server)
Next

Catch ex As Exception
MsgBox("XMLPARSER: CONFIGURATION FILE ERROR " & ex.Message & " "
& ex.Source)
End Try

Nov 21 '05 #7

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.