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

vb.net and xml file

P: n/a
Sam
Hi,
I have a xml file that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<UserName>sorcererdev</UserName>
<Server>192.168.0.1</Server>
<Database>NewWorld</Database>
</UserSettings>
<UserSettings>
<UserName>sa</UserName>
<Server>207.150.0.3</Server>
<Database>master</Database>
</UserSettings>

Is there a 'simple' way ( that is using existing framework's functions)
which would allow finding out easily if a server with a given IP is
stored in the file, and if so, we get the username and database
associated to it.
Or should I write my own file parser ?
Thx

Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Sam
Actually it might be easier if my xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<Server ip="192.168.0.1">
<UserName>sorcererdev</UserName>
<Database>NewWorld</Database>
</Server>
<Server ip="207.150.0.3">
<UserName>sa</UserName>
<Database>master</Database>
</Server>
</UserSettings>
But then how can I find the correct server and its data (usrname +
database)

Nov 21 '05 #2

P: n/a
Sam
Actually it's very simple...just have to use the GetAttribute method.

however i'd like to store this in a dataset, so I do the following:

Dim isoStorage As IsolatedStorageFile
Dim StoreFileNames As String()
Dim StoreFile As String
Dim stmReader As StreamReader
Dim xmlReader As XmlTextReader
Dim ds As New DataSet

Try
isoStorage = IsolatedStorageFile.GetUserStoreForDomain
StoreFileNames =
isoStorage.GetFileNames("UserSettings.xml")

For Each StoreFile In StoreFileNames
If StoreFile = "UserSettings.xml" Then
stmReader = New StreamReader(New
IsolatedStorageFileStream( _
"UserSettings.xml", FileMode.Open,
isoStorage))
xmlReader = New XmlTextReader(stmReader)

While xmlReader.Read()
Select Case xmlReader.Name
Case "UserName"
UserName = xmlReader.ReadString
'xmlReader.GetAttribute(0)
Case "Server"
Srv = xmlReader.ReadString
Case "Database"
Database = xmlReader.ReadString
End Select
End Whil
------> ds.ReadXml(stmReader)
xmlReader.Close()
stmReader.Close()

End If

Next

but I get the error 'The root element is missing'... What's wrong with
my xml file ?

<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<UserName>sorcererdev</UserName>
<Server>192.168.0.1</Server>
<Database>NewWorld</Database>
</UserSettings>

Nov 21 '05 #3

P: n/a
Sam
hum strange my xml file hasn't been posted properly.... Here it is :

<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
<UserName>sorcererdev</UserName>
<Server>192.168.0.1</Server>
<Database>NewWorld</Database>
</UserSettings>

Nov 21 '05 #4

P: n/a
Sam
found out....
xmlReader = New XmlTextReader(stmReader)
ds.ReadXml(stmReader)

removed the first line, and it works

Nov 21 '05 #5

P: n/a
Sam,

And this one
\\\
ds as new dataset
ds.readXML
dim dv as dataview =ds.tables(0).defaultview
dv.rowfilter = "Server = '192.168.0.1'"
if dv.rows.count = 0 then 'it does not exist.
///

Just typed watch typos

I hope this helps,

Cor
"Sam" <sa**************@voila.fr> schreef in bericht
news:11**********************@g14g2000cwa.googlegr oups.com...
found out....
xmlReader = New XmlTextReader(stmReader)
ds.ReadXml(stmReader)

removed the first line, and it works

Nov 21 '05 #6

P: n/a
Assuming you save the XML to a file named "tmp.xml" in the same path as
the following code, you could try something along these lines:

================================================== =
Option Strict

Imports Microsoft.VisualBasic
Imports System
Imports System.xml

Public Class MyProject

Public Shared Sub Main()

Dim IPTest1 As String = "192.168.0.1"
Dim IPTest2 As String = "199.168.1.1"
Dim IPTest3 As String = "199.168.000.001"

Dim x As New XMLDocument
x.load("tmp.xml")
Dim uname as string = _
x.selectSingleNode("/UserSettings/UserName").innerText
Dim srver as string = _
x.selectSingleNode("/UserSettings/Server").innerText
Dim dbase as string = _
x.selectSingleNode("/UserSettings/Database").innerText

Dim ServerFound1 As Boolean = _
x.selectNodes("/UserSettings[Server = '"& IPTest1 & "']").Count > 0

Dim ServerFound2 As Boolean = _
x.selectNodes("/UserSettings[Server = '"& IPTest2 & "']").Count > 0

Dim ServerFound3 As Boolean = _
x.selectNodes("/UserSettings[Server = '"& IPTest2 & "']").Count > 0

Console.WriteLine("Name: " & uname)
Console.WriteLine("Server: " & srver)
Console.WriteLine("Database: " & dbase)

Console.WriteLine()
Console.WriteLine(IPTest1 & " found: " & ServerFound1.ToString)
Console.WriteLine(IPTest2 & " found: " & ServerFound2.ToString)
Console.WriteLine(IPTest3 & " found: " & ServerFound3.ToString)
End Sub

End Class

================================================== =

Note that this finds "192.168.0.1" but not "192.168.000.001" which are
equivalent IP addresses but not equivalent string values
Ralf
"Sam" <sa**************@voila.fr> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
: hum strange my xml file hasn't been posted properly.... Here it is :
:
: <?xml version="1.0" encoding="utf-8"?>
: <UserSettings>
: <UserName>sorcererdev</UserName>
: <Server>192.168.0.1</Server>
: <Database>NewWorld</Database>
: </UserSettings>
:


Nov 21 '05 #7

P: n/a
Sam
thx, but I'm now using a dataset so I'll go for Cor's solution.

Regards

Nov 21 '05 #8

P: n/a
Sam
Cor,

I've got the following error :
Cannot interpret token '.' at position 8.

when I do that :
dv.RowFilter = Format("Server = '{0}'", cboSrv.Text)

cboSrv is "192.168.0.1"

Can you help ?

Nov 21 '05 #9

P: n/a
On 2005-05-11, Sam <sa**************@voila.fr> wrote:
Cor,
Cool... That works. I thought Format was a good practice...
whatever....


You're confusing String.Format with Microsoft.VisualBasic.Format. In VB.Net,
Format by itself will use the VisualBasic.Format call, which is a very different
call.

Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.