locked
xml file being deleted when stream closed RRS feed

  • Question

  • Hi All

    Using VB.net 2008 .net 2 to create an xml file from a dataset

    After the writexml statement the file is created OK
    When the stream is closed the xml file gets deleted????

    code

    Dim

     

    dssettings As New DataSet("SAMSettingsDS")

     

    Dim tblsettings As New DataTable("SAM")

    tblsettings.Columns.Add(

    "dbserver", Type.GetType("System.String"))

    tblsettings.Columns.Add(

    "licencekey", Type.GetType("System.String"))

    tblsettings.Columns.Add(

    "IsEvaluationKey", Type.GetType("System.Boolean"))

    tblsettings.Columns.Add(

    "Port", Type.GetType("System.Int32"))

    tblsettings.Columns.Add(

    "version", Type.GetType("System.String"))

    tblsettings.Columns.Add(

    "serial", Type.GetType("System.String"))

     

    Dim dr As DataRow = tblsettings.NewRow

    dr(

    "dbserver") = "Osmonttech"

    dr(

    "licencekey") = "123456456"

    dr(

    "IsEvaluationKey") = "True"

    dr(

    "Port") = "1500"

    dr(

    "version") = "Full"

    dr(

    "serial") = "123456"

    tblsettings.Rows.Add(dr)

    dssettings.Tables.Add(tblsettings)

     

    Dim filename As String = "C:\Temp\Customers.xml"

     

    ' Create the FileStream to write with.

     

    Dim stream As New System.IO.StreamWriter(filename, True)

     

    ' Write to the file with the WriteXml method.

    dssettings.WriteXml(stream, XmlWriteMode.WriteSchema) - xml file created

    stream.Close()  - this deletes the xml file

    I am lost

    Regards
    Steve

    Thursday, December 3, 2009 7:40 AM

Answers


  • How do you know it is deleting the file???  I ran your code "as-is" without any problems. Maybe it's not creating it in the first place

    but, 2 things to try:

    first, put

    stream.Flush

    just before the stream.Close line



    or use Using:

    Using sw As StreamWriter = New StreamWriter(filename, True)
                dssettings.WriteXml(sw, XmlWriteMode.WriteSchema) '- xml file created
            End Using
    • Proposed as answer by Ji.Zhou Thursday, December 10, 2009 6:36 AM
    • Marked as answer by Ji.Zhou Monday, December 14, 2009 3:00 AM
    Thursday, December 3, 2009 7:52 AM

All replies


  • How do you know it is deleting the file???  I ran your code "as-is" without any problems. Maybe it's not creating it in the first place

    but, 2 things to try:

    first, put

    stream.Flush

    just before the stream.Close line



    or use Using:

    Using sw As StreamWriter = New StreamWriter(filename, True)
                dssettings.WriteXml(sw, XmlWriteMode.WriteSchema) '- xml file created
            End Using
    • Proposed as answer by Ji.Zhou Thursday, December 10, 2009 6:36 AM
    • Marked as answer by Ji.Zhou Monday, December 14, 2009 3:00 AM
    Thursday, December 3, 2009 7:52 AM
  • Hello Steve,


    I cannot reproduce the issue either. Technically, the Stream.Close should not delete the target files. So maybe another issue confuse what we see. If you want to monitor files creation and deletion, you can use Process Monitor to view all file actions.
    http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx



     

    Ji Zhou

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 10, 2009 6:35 AM