vb.net and xml file

S

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
 
S

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)
 
S

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>
 
S

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>
 
S

Sam

found out....
xmlReader = New XmlTextReader(stmReader)
ds.ReadXml(stmReader)

removed the first line, and it works
 
C

Cor Ligthert

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
 
A

_AnonCoward

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


: 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>
:
 
S

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 ?
 
C

Cor Ligthert

Sam,

I never or seldom use that format command why are you not using

dv.RowFilter = "Server = '" & cboSrv.Text "'"

I never saw it in a different way in these newsgroups,

Cor
 
S

Supra

<configuration>
- <#> <PirateChat>
<RealName>Diamond are forever</RealName>
<Email>[email protected]</Email>
<Nick>neetu-18</Nick>
<AltNick>adios</AltNick>
<Pswrd>adios</Pswrd>
<Network>Dalnet</Network>
<Server>irc.dal.net</Server>
</PirateChat>
- <#> <UserModes>
<InvisibleOnServer>1</InvisibleOnServer>
<ReceiveServerMsg>1</ReceiveServerMsg>
<Wallop>1</Wallop>
</UserModes>
- <#> <Misc>
<ShowJoin>0</ShowJoin>
<ShowPart>0</ShowPart>
<ShowQuit>0</ShowQuit>
<ShowBan>0</ShowBan>
<ShowKick>0</ShowKick>
<ShowMode>0</ShowMode>
<ReConnect>False</ReConnect>
<ShowTime>0</ShowTime>
<ShowReJoin>0</ShowReJoin>
<AutoAcceptDccTran>0</AutoAcceptDccTran>
<ShowPvtMsg>0</ShowPvtMsg>
<ShowIgnoreVersion>0</ShowIgnoreVersion>
<ShowIgnorePing>0</ShowIgnorePing>
<ShowIgnoreFinger>0</ShowIgnoreFinger>
<ShowIgnorePage>0</ShowIgnorePage>
<ShowIgnoreUserInfo>0</ShowIgnoreUserInfo>
<ShowIgnoreClient>0</ShowIgnoreClient>
<HideDCC>0</HideDCC>
<Startup>False</Startup>
</Misc>
- <#> <Identd>
<IdentName>djanjo</IdentName>
<IdentSystem>windowXP</IdentSystem>
<IdentPort>113</IdentPort>
</Identd>
</configuration>
 
D

David

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.
 

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