vb.net and xml file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam

    #1

    vb.net and xml file

    Hi,
    I have a xml file that looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <UserSettings >
    <UserName>sorce rerdev</UserName>
    <Server>192.168 .0.1</Server>
    <Database>NewWo rld</Database>
    </UserSettings>
    <UserSettings >
    <UserName>sa</UserName>
    <Server>207.150 .0.3</Server>
    <Database>maste r</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

  • Sam

    #2
    Re: vb.net and xml file

    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>sorce rerdev</UserName>
    <Database>NewWo rld</Database>
    </Server>
    <Server ip="207.150.0.3 ">
    <UserName>sa</UserName>
    <Database>maste r</Database>
    </Server>
    </UserSettings>
    But then how can I find the correct server and its data (usrname +
    database)

    Comment

    • Sam

      #3
      Re: vb.net and xml file

      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 IsolatedStorage File
      Dim StoreFileNames As String()
      Dim StoreFile As String
      Dim stmReader As StreamReader
      Dim xmlReader As XmlTextReader
      Dim ds As New DataSet

      Try
      isoStorage = IsolatedStorage File.GetUserSto reForDomain
      StoreFileNames =
      isoStorage.GetF ileNames("UserS ettings.xml")

      For Each StoreFile In StoreFileNames
      If StoreFile = "UserSettings.x ml" Then
      stmReader = New StreamReader(Ne w
      IsolatedStorage FileStream( _
      "UserSettings.x ml", FileMode.Open,
      isoStorage))
      xmlReader = New XmlTextReader(s tmReader)

      While xmlReader.Read( )
      Select Case xmlReader.Name
      Case "UserName"
      UserName = xmlReader.ReadS tring
      'xmlReader.GetA ttribute(0)
      Case "Server"
      Srv = xmlReader.ReadS tring
      Case "Database"
      Database = xmlReader.ReadS tring
      End Select
      End Whil
      ------> ds.ReadXml(stmR eader)
      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>sorce rerdev</UserName>
      <Server>192.168 .0.1</Server>
      <Database>NewWo rld</Database>
      </UserSettings>

      Comment

      • Sam

        #4
        Re: vb.net and xml file

        hum strange my xml file hasn't been posted properly.... Here it is :

        <?xml version="1.0" encoding="utf-8"?>
        <UserSettings >
        <UserName>sorce rerdev</UserName>
        <Server>192.168 .0.1</Server>
        <Database>NewWo rld</Database>
        </UserSettings>

        Comment

        • Sam

          #5
          Re: vb.net and xml file

          found out....
          xmlReader = New XmlTextReader(s tmReader)
          ds.ReadXml(stmR eader)

          removed the first line, and it works

          Comment

          • Cor Ligthert

            #6
            Re: vb.net and xml file

            Sam,

            And this one
            \\\
            ds as new dataset
            ds.readXML
            dim dv as dataview =ds.tables(0).d efaultview
            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" <samuel.berthel ot@voila.fr> schreef in bericht
            news:1115807297 .167288.221130@ g14g2000cwa.goo glegroups.com.. .[color=blue]
            > found out....
            > xmlReader = New XmlTextReader(s tmReader)
            > ds.ReadXml(stmR eader)
            >
            > removed the first line, and it works
            >[/color]


            Comment

            • _AnonCoward

              #7
              Re: vb.net and xml file

              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.Visua lBasic
              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.00 1"

              Dim x As New XMLDocument
              x.load("tmp.xml ")


              Dim uname as string = _
              x.selectSingleN ode("/UserSettings/UserName").inne rText
              Dim srver as string = _
              x.selectSingleN ode("/UserSettings/Server").innerT ext
              Dim dbase as string = _
              x.selectSingleN ode("/UserSettings/Database").inne rText

              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.WriteLi ne("Name: " & uname)
              Console.WriteLi ne("Server: " & srver)
              Console.WriteLi ne("Database: " & dbase)

              Console.WriteLi ne()
              Console.WriteLi ne(IPTest1 & " found: " & ServerFound1.To String)
              Console.WriteLi ne(IPTest2 & " found: " & ServerFound2.To String)
              Console.WriteLi ne(IPTest3 & " found: " & ServerFound3.To String)
              End Sub

              End Class

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



              Note that this finds "192.168.0. 1" but not "192.168.000.00 1" which are
              equivalent IP addresses but not equivalent string values


              Ralf


              "Sam" <samuel.berthel ot@voila.fr> wrote in message
              news:1115803743 .968124.92700@o 13g2000cwo.goog legroups.com...
              : hum strange my xml file hasn't been posted properly.... Here it is :
              :
              : <?xml version="1.0" encoding="utf-8"?>
              : <UserSettings >
              : <UserName>sorce rerdev</UserName>
              : <Server>192.168 .0.1</Server>
              : <Database>NewWo rld</Database>
              : </UserSettings>
              :




              Comment

              • Sam

                #8
                Re: vb.net and xml file

                thx, but I'm now using a dataset so I'll go for Cor's solution.

                Regards

                Comment

                • Sam

                  #9
                  Re: vb.net and xml file

                  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 ?

                  Comment

                  • David

                    #10
                    Re: vb.net and xml file

                    On 2005-05-11, Sam <samuel.berthel ot@voila.fr> wrote:[color=blue]
                    > Cor,
                    > Cool... That works. I thought Format was a good practice...
                    > whatever....[/color]

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

                    Comment

                    Working...