Need help coding vb.net XML file lookup

X

XML Beginner

I have an XML file that contains values that my application needs, so it
knows which database to connect to. It also contains a configuration option
so that I can specify which node to return values from. For example, the XML
file is meant to return the values from node"config2", because the
"configoption" node's value is that.

I'm extremely new to XML and the code that accesses it, but from what I
could gather from materials read, I got this to work, which correctly
returns the database/server values from the XML file into VB.Net variables.

I'm glad I was able to get it to work, but surely there must be a better way
to write vb.net code to do this (?). Can anybody please help me with better
vb.net code to query the xml file? There's a real business need for the
"configoption" part, but the vb.net code and rest of the xml file is fair
game and can be modified as you see fit.

For example, I'm using the InnerText property...that's can't be the best way
right? From the help file, it says it returns the concatenated values of the
node and all its children. Since that node has only 1 child, this works for
now. But this is where I need help...and some better coding help which I
hope will help me to better understand. Thanks for any assistance, as I am a
total newbie to XML and I believe that sample code, within the context of a
real world need, is a great learning tool. Any suggestions are welcome.
Thanks.

Here's the XML file...

<?xml version="1.0" encoding="utf-8" ?>
<config>
<configoption>config2</configoption>
<config1>
<SQLServer>Server1</SQLServer>
<SQLDatabase>Database1</SQLDatabase>
</config1>
<config2>
<SQLServer>Server2</SQLServer>
<SQLDatabase>Database2</SQLDatabase>
</config2>
</config>

Sub SampleXMLGetValues()

' Return the connection string info from the XML file
Dim strSqlServer As String
Dim strSqlDatabase As String
Dim szXMLFile As String = "D:\_Test\SqlConfig.xml"
' Load the XML file
Dim oXMLDoc As New System.XML.XmlDocument
Dim oNode As XmlNode
oXMLDoc.Load (szXMLFile)
oNode = oXMLDoc.GetElementsByTagName("config").Item(0)

' Based on the configuration option value, decide which node to retrieve
values from
Dim strConfigOption As String
Dim n As XmlNode
For Each n In oNode
If Strings.StrComp(n.Name, "configoption", CompareMethod.Text) = 0
Then
strConfigOption = n.InnerText
End If
Next

' Set the SQL database values
For Each n In oNode
If Strings.StrComp(n.Name, strConfigOption, CompareMethod.Text) = 0
Then
' Return the values within each relevant node of the config
option
strSqlServer = n.Item("SQLServer").InnerText
strSqlDatabase = n.Item("SQLDatabase").InnerText
End If
Next

End Sub
 
M

Michael_R_Banks

Have you looked at using dataset(s) with DataSet.ReadXML(path) ? I
just used this with great success in my first XML-enabled program.
Very simple to get your arms around.
 
X

XML Beginner

Good point! However I'm hoping someone will help show me the way to do this
by direct query.
Thanks.
 

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

Similar Threads


Top