Reading XML File

G

Guest

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
 
G

Guest

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?
 
G

Guest

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
 
G

Guest

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.
 
A

Abubakar

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
 
M

Markus Bauer

Hi,

did you try:

xml_nodelist = xml_doc.SelectNodes("/CAREKEEPER")

Perhaps it is a case sensitive issue?

Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top