简体   繁体   中英

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?

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

The FileStream class implements IDisposable so it needs to be closed and disposed. (Avoid leaks of unmanaged resources, file handles and such)

The Using block will handle this even if there is an error.

This answer is the same as @Vlam 's answer with the addition of the Using block. @Vlam is the one who diagnosed the error so please accept that answer but do add the Using block.

Private Sub WriteXmlToFile(RoboDataSet As DataSet)
    If RoboDataSet Is Nothing Then
        MessageBox.Show("The DataSet does not exist.")
        Return
    End If
    '**EDIT**
    For Each t As DataTable In RoboDataSet.Tables
        Debug.Print(t.Rows.Count.ToString)
    Next
    '**END EDIT**

    Dim filename As String = "c:\data\write4.xml"
    Using Stream As New System.IO.FileStream(filename, System.IO.FileMode.Create)
        RoboDataSet.WriteXml(Stream)
    End Using
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM