Help: creating a backup file using xml

ebbinger_413

Member
Joined
Apr 11, 2008
Messages
6
Programming Experience
1-3
I have been working on a medium scale program for the nds-scene for some time now. I had the first version completed and was created in vb.net 2003 and working perfectly. I am now in the process of updating the program to .net 2.0 and am using vs 2005. The old version had a backup feature which would backup a listview control to a simple ascii text file.

I am changing the backup file to an xml file now and am using the following code to do so:

VB.NET:
' show the export dialog for user to choose a location to save the backup
        dlg_export.ShowDialog()
        Dim export As XmlTextWriter = New XmlTextWriter(dlg_export.FileName, System.Text.Encoding.UTF8)
        export.WriteStartDocument()
        export.Formatting = Formatting.Indented
        export.Indentation = 4
        export.WriteDocType("HomebrewManagerBackup", Nothing, Nothing, Nothing)
        export.WriteComment("Created on: " & Now())
        For Each item As ListViewItem In lst_main.Items
            export.WriteStartElement("Item")
            For Each subitem As ListViewItem.ListViewSubItem In item.SubItems
                export.WriteElementString("Subitem", subitem.Text)
            Next
            export.WriteEndElement()
        Next
        export.WriteElementString("MOTD", My.Settings.motd_message)
        export.WriteEndDocument()
        export.Close()

I am looking to get an output similar to below:

VB.NET:
<?xml version=1? encoding=utf-8>
<doctype homebrewmanagerbackup>
<--created on dateandtimehere-->
<item>
     <subitem></subitem>
</item>
<motd></motd>

But i am getting the following error:

Token StartElement in state Epilog would result in an invalid XML document.

This is the first time I am using vb to write xml so there might be something im doing wrong, but from every site or book ive seen around it looks to be correctly codded. I also could not find anything as to this error in regards to xml with vb online anywhere.

- Please help !!!
 
Maybe you should starting reading about the Xml format first? Xml can only have one root element.
 
ahh...thank you, an answer none the less...

i didnt even realize that...guess if i wanna do it that way i have to use the fragment feature so that i dont have to conform the xml standards...

thanks for the response even if you werent really trying to help

- cant believe i didnt see that :mad:
 
Solved:

Problem is solved, just thought i would post my code snipplets if anyone was interested:

Code to backup listview control
VB.NET:
' make sure there is something to backup
        If lst_main.Items.Count <> 0 Or My.Settings.motd_message <> "" Then
            ' show the export dialog for user to choose a location to save the backup
            dlg_export.ShowDialog()
            ' if the user didn't cancel out of the form
            If System.IO.File.Exists(dlg_export.FileName) Then
                ' create an xml document with their filename
                Dim export As XmlTextWriter = New XmlTextWriter(dlg_export.FileName, System.Text.Encoding.UTF8)
                export.WriteStartDocument()
                export.Formatting = Formatting.Indented ' format is going to be indented
                export.Indentation = 4 ' indenting is going to 4 spaces
                export.WriteDocType("HomebrewManagerBackup", Nothing, Nothing, Nothing) ' set the doctype
                export.WriteComment("Created on: " & Now()) ' add a comment with the date
                export.WriteStartElement("Backup") ' begin the element backup
                For Each item As ListViewItem In lst_main.Items ' for each item in the list
                    export.WriteStartElement("Item") ' add an element for that item
                    For Each subitem As ListViewItem.ListViewSubItem In item.SubItems ' and for each subitem in that item
                        export.WriteElementString("Subitem", subitem.Text) ' add an element for that subitem
                    Next
                    export.WriteEndElement() ' end item element
                Next
                export.WriteElementString("MOTD", My.Settings.motd_message) ' add the motd to the backup
                export.WriteEndElement() ' end backup (root) element
                ' then close the document and xml writer
                export.WriteEndDocument()
                export.Close()
            End If
        Else ' nothing to backup let user know
            MsgBox("There is nothing to backup! Please add entries!", vbOKOnly, "Empty Data!")
        End If

Code to import the backed up data into the listview control
VB.NET:
' show the import dialog for user to select a file
        dlg_import.ShowDialog()
        ' if user didnt just cancel then
        If System.IO.File.Exists(dlg_import.FileName) Then
            ' first we need to clear out the listview control of all items
            lst_main.Items.Clear()
            ' open the xml file for reading
            Dim import As XmlTextReader = New XmlTextReader(dlg_import.FileName)
            Dim newitem As New ListViewItem ' create new listview item to add in data
            Dim count As Integer = 0 ' variable for counting subitems
            Do While import.Read() ' while reading the xml file
                If import.NodeType = XmlNodeType.Element Then ' if reading an element
                    If import.Name = "Item" Then ' if element is an item
                        ' remove data from the listview item so we can add new item
                        ' and then recreate the item and set the count to zero
                        newitem = Nothing
                        newitem = New ListViewItem
                        count = 0
                    End If
                    If import.Name = "Subitem" Then ' if element is subitem check the count
                        If count = 0 Then ' if count is zero than its the firt subitem
                            ' so we can use the string from the element to set the first column
                            ' of the listviewitem
                            newitem.Text = import.ReadString.ToString
                        Else ' otherwise count isnt zero and it isnt the first subitem
                            ' so simply add the subitem to the collection in the listviewitem
                            newitem.SubItems.Add(import.ReadString.ToString)
                        End If
                        ' then add to count so we can keep track of subitems
                        count = count + 1
                    End If
                    If import.Name = "MOTD" Then ' if the element is motd
                        ' set the app settings motd variable to the element string
                        My.Settings.motd_message = import.ReadString
                    End If
                ElseIf import.NodeType = XmlNodeType.EndElement Then ' if reading an end element
                    If import.Name = "Item" Then ' and end element is end item
                        ' add the created listviewitem to the listview control
                        lst_main.Items.Add(newitem)
                    End If
                End If
            Loop
            ' finally close the file
            import.Close()
        End If

Example xml file (can have unlimited items and subitems but only 1 motd)
VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HomebrewManagerBackup>
<!--Created on: 4/13/2008 6:38:20 PM-->
<Backup>
    <Item>
        <Subitem>0</Subitem>
        <Subitem>DSOrganize</Subitem>
        <Subitem>download a program here</Subitem>
        <Subitem>gimme the program</Subitem>
        <Subitem>get it from here now</Subitem>
        <Subitem>04/13/2008</Subitem>
        <Subitem>1.2 Beta</Subitem>
        <Subitem>12.2</Subitem>
        <Subitem>Games</Subitem>
        <Subitem>12</Subitem>
        <Subitem>down get it from here now@</Subitem>
    </Item>
    <Item>
        <Subitem>1</Subitem>
        <Subitem>DSPaint</Subitem>
        <Subitem>Paint program for ds foo</Subitem>
        <Subitem>get me here</Subitem>
        <Subitem>again, right here!</Subitem>
        <Subitem>04/13/2008</Subitem>
        <Subitem>3.0C</Subitem>
        <Subitem>1325</Subitem>
        <Subitem>Applications</Subitem>
        <Subitem>1</Subitem>
        <Subitem>down again, right here!@</Subitem>
    </Item>
    <Item>
        <Subitem>2</Subitem>
        <Subitem>Moonshell</Subitem>
        <Subitem>media player for ds</Subitem>
        <Subitem>media now</Subitem>
        <Subitem>get maybe</Subitem>
        <Subitem>04/13/2008</Subitem>
        <Subitem>34</Subitem>
        <Subitem>3</Subitem>
        <Subitem>System</Subitem>
        <Subitem>12</Subitem>
        <Subitem>down get maybe@</Subitem>
    </Item>
    <MOTD>just gimme something to do!!!
</MOTD>
</Backup>

Thanks for the pointing out of my stupid mistake:p
 
Back
Top