By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,704 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,704 IT Pros & Developers. It's quick & easy.

Need help coding vb.net XML file lookup

P: n/a
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
Feb 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Feb 7 '07 #2

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

"Michael_R_Banks" <mi***********@satx.rr.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
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.

Feb 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.