Reading XML

JDP

New member
Joined
Sep 19, 2006
Messages
2
Programming Experience
Beginner
Hello, simple question how can I best read in the XML below? I've tried several different ways, it either doesn't work or errors out on me. I've found a way that works, but I need to completely change my xml config, which I don't want to do. Thanks in advance!
VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<config>
    <client name="MyClient">
    <url>/myurl/location/</url>
    <webservers>
        <serverpath>C:\Program Files\etc\etc\etc</serverpath>
        <logType>IIS</logType>
        <server>SERVER01</server>
        <server>SERVER02</server>
    </webservers>
    </client>
    <client name="Another Client">
    <url>/client/stats/</url>
    <webservers>
        <serverpath>C:\Program Files\etc\etc\etc</serverpath>
        <logType>IIS</logType>
        <server>SERVER201</server>
        <server>SERVER333</server>
    </webservers>
    </client>    
</config>
Basically, I'm trying to read this in, and for each client perform another action. It shouldn't be this tough - but I'm a rookie in general and even more so when it comes to xml handling.
 
Some hints on reading
VB.NET:
Dim xdoc As New Xml.XmlDocument
xdoc.Load("temp.xml")
For Each client As Xml.XmlNode In xdoc.SelectNodes("/config/client")
  For Each server As Xml.XmlNode In client.SelectNodes("webservers/server")
    MsgBox(client.Attributes("name").Value & ":" & server.InnerText)
  Next
Next
 
You can also load it as a Dataset. Here is an example form I have in one of my projects.. it reads an xml file in and then allows you to drill into the structure. You should find it a useful visual aid:
 

Attachments

  • XMLData.zip
    3.6 KB · Views: 17
You can also create a vb class that reflects the xml structure and use serialization for easier working with class instances instead of xml nodes (when you are not altering the xml structure). Helpful for this could be the Xsd.exe utility to generate schema and vb class from the xml data.
 
Oi, thanks a lot guys - I was in the right area, but I had a few key things missing and this tied it all together. I appreciate it!
 
Back
Top