vb.netwritexml

trying to writeXml in VB.net


UPDATE Thanks vlam, I am now writing to Stream as you suggested, I get an XML file and no more errors, my XML file only has this, no data,

<?xml version="1.0"?>
<RoboDataSet/>

Why do I not have data?

I have a datagridview and a dataset on a form. The form opens, I type data in the first 2 rows, then I click a write XML button I added to the form. Code for the button and the WriteXML is here. It creates a file, which is empty. But when it tries to execute the RoboDataSet.WriteXml(filename) command, I get an error.

After I type data in the DataGridView is it saved in the DataGridView and the DataSet at that point?

I have a message box, displays we have data, so I believe the data I entered is in the dataset. Then I step the code and I see a file created in c:\data, then on the RoboDataSet.WriteXml(filename) command I get this error;

So a few questions, why do I get the error being used, it is the same process trying to writexml, correct?

I tried 2 different ways, based on MS examples I saw,

Example 1
Dim stream As New System.IO.FileStream _
    (filename, System.IO.FileMode.Create)
 thisDataSet.WriteXml(stream)

Example 2
Dim filename As String = "XmlDoc.xml"
thisDataSet.WriteXml(filename)

and here is my code that is not working;

Private Sub WriteXml_Click(sender As Object, e As EventArgs) Handles WriteXml.Click
    WriteXmlToFile(RoboDataSet)
End Sub

Private Sub WriteXmlToFile(RoboDataSet As DataSet)
    If RoboDataSet Is Nothing Then
        MessageBox.Show("dataset empty")
    Else
        MessageBox.Show("We have data")
    End If
    Dim filename As String = "c:\data\write4.xml"
    Dim Stream As New System.IO.FileStream _
        (filename, System.IO.FileMode.Create)
    RoboDataSet.WriteXml(filename)




End Sub

Any help would be great, thanks

Thanks vlam, I am now writing to Stream as you suggested, I get an XML file and no more errors, my XML file only has this, no data,

Why do I not have data?


Solution

  • Should be writing to a stream and not the filename. Also only write if dataset is not nothing.

    Private Sub WriteXmlToFile(RoboDataSet As DataSet)
        If RoboDataSet Is Nothing Then
            MessageBox.Show("dataset empty")
        Else
            MessageBox.Show("We have data")
            Dim filename As String = "c:\data\write4.xml"
            Dim Stream As New System.IO.FileStream(filename, System.IO.FileMode.Create)
            RoboDataSet.WriteXml(Stream)
        End If
    End Sub