VB.Net同时保存单独的XML文件并覆盖数据

时间:2016-02-24 06:26:04

标签: xml vb.net

这个问题是我previous question的延续。 基本上,该应用程序允许用户执行以下操作:

  • 选择在其计算机上找到的1个或多个Excel文件
  • 然后将文件转换为xml并保存。

我现在遇到的问题是,我已经弄清楚如何保存多个文件,但保存的每个文件都不会删除旧数据。

如何删除或覆盖旧的xml数据?

问题示例:

  

该应用会保存3个成功转换的文件,标题为:xml1.xml,   xml2.xml和xml3.xml

     
      
  • xml1只包含第一个excel工作簿中的数据
  •   
  • xml2包含来自excel工作簿1和工作簿2的数据
  •   
  • xml3包含来自所有3个工作簿的数据
  •   
     

每个xml文件应该只包含当前excel文件中的数据   读。因此,xml1将拥有第一个文件,xml2将拥有第二个文件,等等   上。

我已经尝试清理,刷新和处理数据集,xmlreader和 读取每个文件后的内存流,但这似乎不起作用。我肯定做错了什么。

感谢任何帮助/建议。

我的大多数代码都在下面:

'Load Excel File Button
            Private Sub loadFileBtn_Click(sender As Object, e As EventArgs) Handles loadFileBtn.Click
                'choose excel file
                'file dialog box properties
                OpenFileDialog1.Filter = "Excel Files (*.xls, * .xlsx)|*.xls;*.xlsx"
                OpenFileDialog1.FilterIndex = 2
                OpenFileDialog1.InitialDirectory = "C:\desktop\"
                Dim checkOpenDialog As DialogResult = OpenFileDialog1.ShowDialog()
                If (checkOpenDialog = DialogResult.Cancel) Then
                ElseIf (checkOpenDialog = DialogResult.None) Then
                ElseIf checkOpenDialog = DialogResult.OK Then
                    For Each fileCount In OpenFileDialog1.FileNames
                        Try
                            Dim countBW As Integer
                            While countBW < OpenFileDialog1.FileNames.Count
                                Dim worker As New BackgroundWorker()
                                AddHandler worker.DoWork, New DoWorkEventHandler(AddressOf worker_DoWork)
                                worker.RunWorkerAsync(countBW)
                                countBW += 1
                            End While
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try
                    Next fileCount
                End If
            End Sub
    Private Sub worker_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            myFilePath = OpenFileDialog1.FileNames
            For Each excelFile In myFilePath
                For loadedPercent = 0 To 100
                    formLbl.Text = loadedPercent
                    BackgroundWorker1.ReportProgress(loadedPercent)
                    Thread.Sleep(200)
                    formLbl.Refresh()
                Next
                excelConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFile + ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1;""")
                excelConn.Open()
                myAdapter = New OleDbDataAdapter("select * from [Sheet1$]", excelConn)
                myAdapter.Fill(myDS)
                excelConn.Close()
                myAdapter.Dispose()
                '<----- CREATE XML --------------->
                ''create memorystream that will store xml from dataset 
                Dim xMemStream As MemoryStream = New MemoryStream()
                myDS.WriteXml(xMemStream)
                myDS.Clear()
                myDS.Dispose()
                xMemStream.Seek(0, SeekOrigin.Begin)
                ''create xmlreader that will read xml from memorystram into xdocument/xelement
                Dim xReader As XmlReader = XmlReader.Create(xMemStream)
                xEleDoc = XElement.Load(xReader)
                xReader.Close()
                xReader.Dispose()
                xMemStream.Close()
                xMemStream.Flush()
                '<---- MANIPULATE XML -->
               'all the xml stuff from my last question goes here
Dim tmpQuery As IEnumerable(Of XElement) = From i In xDoc.Elements() Select i
            xQuery = tmpQuery
            xDoc = New XDocument(New XDeclaration("1.0", "ASCII", "yes"), orderfile)
            Dim tmpXDoc As XElement = XElement.Parse(xDoc.ToString)
            'change empty tags to shorthand
            newXdoc = ShorthandEmpty(tmpXDoc)
            'add encoding / declaration
            xDoc = New XDocument(New XDeclaration("1.0", "ASCII", "yes"), newXdoc)
            countFiles += 1
            xDoc.Save("C:\desktop\xml" & countFiles & ".xml")
        Next excelFile

1 个答案:

答案 0 :(得分:0)

花了一些时间来解决这个问题。为了清除旧的xml数据,然后保存每个文件的新数据,这意味着要弄清楚如何清除/覆盖previous solution中的数据。

直到今天,我才意识到旧数据实际存储在名为orderfile的xElement中。一旦我知道需要清除的内容,只需要一行简单的代码即可。

解决方案:

orderfile.RemoveAll()