Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] DataSet and XML files

[VB.NET 2008] DataSet and XML files

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpxmlannouncementworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hi, in my application (running in Windows CE) I use a XML file to store some configuration data that, at runtime, I load in a DataSet. To read and to store these data I use the following code:

    Private Shared Function SetCfgData() As Boolean

    Try
      Dim fs As New FileStream(CGlobali.CfgDataFileComplName, FileMode.Open)
      Dim xr As XmlReader = System.Xml.XmlReader.Create(fs)
    
      'CGlobali.CfgData is a DataSet
      CGlobali.CfgData.Clear()
      CGlobali.CfgData.ReadXml(xr)
    
      xr.Close()
      fs.Close()
    
    Catch ex As System.IO.FileNotFoundException
      MessageBox.Show("File " & CGlobali.CfgDataFileComplName & " not found.")
      Return False
    Catch ex As XmlException
      MessageBox.Show("XmlException occured: " & ex.ToString)
      Return False
    End Try
    
    Return True
    

    When the user modifies these data, they are automatically updated in the DataSet and saved in the file, at least that's what I would do... To save the data from the DataSet to the file I do this:

    Private Shared Function UpdateCgfFile() As Boolean

    Try
      Dim StreamEncoding As Encoding
      StreamEncoding = Encoding.Unicode
      Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)
    
      CGlobali.CfgData.WriteXml(cfgfile)
    
      cfgfile.Close()
    
      Return True
    Catch ex As Exception
      Return False
    End Try
    

    End Function

    What I would get with the above code is to update existing fields in the xml file with the new values, but what I get is that the same fields are added to the existing ones. This is the "original" file:

    10.0.0.85
    502
    
    15000
    5000
    5000
    

    and this is the file after a save:

    10.0.0.85
    502
    
    15000
    5000
    5000
    
    
    192.168.1.10
    502
    15000
    5000
    5000
    

    Obviously this is not the result I want to achieve, I do not want to du

    L 1 Reply Last reply
    0
    • S steve_9496613

      Hi, in my application (running in Windows CE) I use a XML file to store some configuration data that, at runtime, I load in a DataSet. To read and to store these data I use the following code:

      Private Shared Function SetCfgData() As Boolean

      Try
        Dim fs As New FileStream(CGlobali.CfgDataFileComplName, FileMode.Open)
        Dim xr As XmlReader = System.Xml.XmlReader.Create(fs)
      
        'CGlobali.CfgData is a DataSet
        CGlobali.CfgData.Clear()
        CGlobali.CfgData.ReadXml(xr)
      
        xr.Close()
        fs.Close()
      
      Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("File " & CGlobali.CfgDataFileComplName & " not found.")
        Return False
      Catch ex As XmlException
        MessageBox.Show("XmlException occured: " & ex.ToString)
        Return False
      End Try
      
      Return True
      

      When the user modifies these data, they are automatically updated in the DataSet and saved in the file, at least that's what I would do... To save the data from the DataSet to the file I do this:

      Private Shared Function UpdateCgfFile() As Boolean

      Try
        Dim StreamEncoding As Encoding
        StreamEncoding = Encoding.Unicode
        Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)
      
        CGlobali.CfgData.WriteXml(cfgfile)
      
        cfgfile.Close()
      
        Return True
      Catch ex As Exception
        Return False
      End Try
      

      End Function

      What I would get with the above code is to update existing fields in the xml file with the new values, but what I get is that the same fields are added to the existing ones. This is the "original" file:

      10.0.0.85
      502
      
      15000
      5000
      5000
      

      and this is the file after a save:

      10.0.0.85
      502
      
      15000
      5000
      5000
      
      
      192.168.1.10
      502
      15000
      5000
      5000
      

      Obviously this is not the result I want to achieve, I do not want to du

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      steve_9496613 wrote:

      Can someone tell me what I'm doing wrong?

      Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)

      What does the second parameter in the StreamWriter do?

      MSDN[^] states:

      true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      S 1 Reply Last reply
      0
      • L Lost User

        steve_9496613 wrote:

        Can someone tell me what I'm doing wrong?

        Dim cfgfile As New System.IO.StreamWriter(CGlobali.CfgDataFileComplName, True, StreamEncoding)

        What does the second parameter in the StreamWriter do?

        MSDN[^] states:

        true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        ...ops... :sigh: ...next time I'll be more careful... Thanks Eddy

        L 1 Reply Last reply
        0
        • S steve_9496613

          ...ops... :sigh: ...next time I'll be more careful... Thanks Eddy

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You're welcome :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups