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

时间:2023-01-14 11:08:41

This question is a continuation of my previous question. Basically, the application let's the user do the following:

这个问题是我上一个问题的延续。基本上,应用程序让用户执行以下操作:

  • Select 1 or many excel files found on their computer
  • 选择在其计算机上找到的1个或多个Excel文件

  • The file(s) are then converted to xml and saved.
  • 然后将文件转换为xml并保存。

The problem I'm having now is, I've figured out how to save multiple files, but each file that is saved isn't deleting the old data.

我现在遇到的问题是,我已经想出了如何保存多个文件,但保存的每个文件都没有删除旧数据。

How do I delete or overwrite the old xml data?

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

Example of Problem:

问题示例:

The app saves 3 successfully converted files titled: xml1.xml, xml2.xml, and xml3.xml

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

  • xml1 has only the data from the first excel workbook
  • xml1只包含第一个excel工作簿中的数据

  • xml2 has both data from excel workbook 1 and workbook 2
  • xml2包含来自excel工作簿1和工作簿2的数据

  • xml3 has data from all 3 workbooks
  • xml3包含来自所有3个工作簿的数据

Each xml file should only have data from the excel file it's currently reading. So, xml1 would have the first file, xml2 the second, and so on.

每个xml文件应该只包含当前正在读取的excel文件中的数据。因此,xml1将拥有第一个文件,xml2将拥有第二个文件,依此类推。

I've tried clearing, flushing, and disposing of the dataset, xmlreader, and memorystream after each file is read, but that doesn't seem to work. I'm definitely doing something wrong.

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

Any help/suggestions are appreciated.

任何帮助/建议表示赞赏。

Most of my code is below:

我的大多数代码如下:

'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 个解决方案

#1


0  

It took some toying around to figure this out. In order to clear the old xml data and then save the new data for each file, that meant figuring out how to clear/overwrite the data in my previous solution.

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

This didn't click until today, when I realized that the old data was actually stored in an xElement called orderfile. A simple 1 line of code is all that was necessary once I knew what I needed to clear.

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

Solution:

orderfile.RemoveAll()

#1


0  

It took some toying around to figure this out. In order to clear the old xml data and then save the new data for each file, that meant figuring out how to clear/overwrite the data in my previous solution.

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

This didn't click until today, when I realized that the old data was actually stored in an xElement called orderfile. A simple 1 line of code is all that was necessary once I knew what I needed to clear.

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

Solution:

orderfile.RemoveAll()