Simple XML question

B

Brian Henry

I have an XML file formated as this

<?xml version="1.0" encoding="utf-8" ?>
<settings>
<databaseServer>Dell5100</databaseServer>
<defaultDatabase>bene</defaultDatabase>
</settings>

relateively simple file... I want to be able to say which attribute of the
settings group I want to read in a settings read function and return it...
how would i do this? below is the code I have so far, but it requires an
index of the node to read.. how would i specify that i want the database
server item or the defaultdatabase item? i cant by index since im asking for
it by a string value ... here is my code i have, any help would be nice,
thanks!

' reads a setting from xml settings file
Public Shared Function ReadSetting(ByVal settingName As String) As String
Try
' I want to find the attribute named with the name specified in settingname
and return it to the user out of the settings group of the XML file
XMLDocument = New XmlDocument
XMLDocument.Load(bdb.applicationPath & "\benesettings.xml")
XMLNode = XMLDocument.SelectSingleNode("/settings")
Debug.WriteLine("XML READ: " & XMLNode.ChildNodes.Item(0).InnerText)
' here is where it wants the index... i put zero in for an example to test
the load but now i need to find the attribute value by the settingname
specified

Catch ex As Exception
MessageBox.Show("XML Load Error" & ControlChars.CrLf & ex.Message)
End Try

End Function
 
B

Brian Henry

I just made a change and got this

Public Shared Function ReadSetting(ByVal settingName As String) As String

Try

XMLDocument = New XmlDocument

XMLDocument.Load(bdb.applicationPath & "\benesettings.xml")

XMLNode = XMLDocument.SelectSingleNode("/settings/" & settingName)

Debug.WriteLine("XML READ: " & XMLNode.InnerText)



Catch ex As Exception

MessageBox.Show("XML Load Error" & ControlChars.CrLf & ex.Message)

End Try



End Function



does that sound right to you guys? it seems to work for me... thanks!
 
C

Cor

Hi Brian,

Why using a xmldocument file while a xmldataset file is so easy to handle?

Cor
 
B

Brian Henry

duno, thats why i was asking what was the best way to do it :) i've never
really worked with XML before until this
 
C

Cor

Hi Brian,

Than try first the dataset methode.

(You have also the readxml and the loadxml, that are different xml
documents)

Start creating the dataset from your solution explorer by opening it, just
the same as a form.

And only use "elements".

You can read a dataset with dataset.readxml(file) and write it with
dataset.writexml(file).

The advantage is that you have direct a table in your program when you use
it, which you can reach with every method from a dataset.

Cor
 

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