1
這個問題是我的previous question的延續。 基本上,該應用程序讓我們的用戶做到以下幾點:VB.Net保存單獨的XML文件,並同時覆蓋數據
- 選擇1個或多個練成自己的電腦
- 文件(S)上找到的文件,然後轉換成XML並保存。
我現在遇到的問題是,我已經想出瞭如何保存多個文件,但保存的每個文件都不會刪除舊數據。
如何刪除或覆蓋舊的XML數據?問題的
實施例:
該應用保存3成功地轉換標題文件:xml1.xml, xml2.xml和xml3.xml
- XML1僅具有從數據第一個excel工作簿
- xml2同時具有來自excel工作簿1和工作簿2的數據
- xml3具有來自所有3個工作簿的數據
每個xml文件應該只有excel文件中的數據,它當前的讀數爲 。所以,xml1會有第一個文件,第二個xml2,等等 。
我試着清除,沖洗和數據集,XMLReader的處置,以及每個文件後 MemoryStream的閱讀,但似乎並沒有工作。我肯定做錯了什麼。
任何幫助/建議表示讚賞。
我的大部分代碼如下:
'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