Results 1 to 2 of 2

Thread: Dataset - writing to XML / readinf from XML to dataset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    Dataset - writing to XML / readinf from XML to dataset

    I use a Dataset object containing 2 tables to read and write settings. Using the following code I write the XML file:

    VB Code:
    1. Sub SaveSettings()
    2.         'Purpose: Save settings to XML
    3.  
    4.         Dim strFileName As String = My.Application.Info.Title & "Init.xml"
    5.  
    6.         'Open the file if it exists
    7.         If System.IO.File.Exists(My.Application.Info.DirectoryPath & "\" & strFileName) Then
    8.             frmMain.dsProgramSettings.ReadXml(My.Application.Info.DirectoryPath & "\" & strFileName)
    9.         End If
    10.  
    11.         'Create Dataset Row for Settings and assign values
    12.         Dim rowSettings As DataRow = frmMain.dsProgramSettings.Tables("Settings").NewRow
    13.  
    14.         rowSettings("subDirs") = frmMain.chkSubDirs.CheckState
    15.         rowSettings("SaveDirs") = frmMain.chkSave.CheckState
    16.  
    17.         ' Add the record to the settings table
    18.         frmMain.dsProgramSettings.Tables("settings").Rows.Add(rowSettings)
    19.  
    20.         'Create dataset Row for Directories to be saved
    21.         If SaveDirs And Not SelectedDirectories Is Nothing Then
    22.             Dim Dir As Short
    23.  
    24.             For Dir = 0 To UBound(SelectedDirectories)
    25.                 Dim rowDirs As DataRow = frmMain.dsProgramSettings.Tables("Dirs").NewRow
    26.                 rowDirs("Dirname") = SelectedDirectories(Dir).ToString
    27.                 frmMain.dsProgramSettings.Tables("Dirs").Rows.Add(rowDirs)
    28.             Next Dir
    29.  
    30.         End If
    31.  
    32.         ' Update the XML file
    33.         frmMain.dsProgramSettings.WriteXml(strFileName)
    34.  
    35.     End Sub

    When I write data to the second table ("Dirs", containing 1 column "DirName")
    the XML looks like this

    VB Code:
    1. <?xml version="1.0" standalone="yes"?>
    2. <ProgramSettings>
    3.   <Settings>
    4.     <SubDirs>true</SubDirs>
    5.     <SaveDirs>Checked</SaveDirs>
    6.   </Settings>
    7.   <Dirs>
    8.     <DirName>c:\temp</DirName>
    9.   </Dirs>
    10.   <Dirs>
    11.     <DirName>c:\install</DirName>
    12.   </Dirs>
    13. </ProgramSettings>

    But I want it be like:

    VB Code:
    1. <?xml version="1.0" standalone="yes"?>
    2. <ProgramSettings>
    3.   <Settings>
    4.     <SubDirs>true</SubDirs>
    5.     <SaveDirs>Checked</SaveDirs>
    6.   </Settings>
    7.   <Dirs>
    8.     <DirName>c:\temp</DirName>
    9.     <DirName>c:\install</DirName>
    10.   </Dirs>
    11. </ProgramSettings>

    I clearly am missing something cause i can't get it right! Any pointers?

    Also, I want to write a XHTML file. How would I go about writing the extra tags needed for that (like <html></html> etc).

    Thanks!

    EDIT:
    I just realized that this might normal behaviour cause for the settings table I am writing one row with multiple (two) columns whereas for the dirs table I am writing multiple rows of one column each.
    So is the above xml represention correct ?
    Last edited by SammyWaslow; Mar 21st, 2006 at 10:43 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    88

    Re: writing XML file - rows for one section & XHTML

    Could anyone please shed some light on this?
    Using Visual Studio 2005 / Framework 2.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width