[英]How to copy complete node from xml file and create multiple node by changing names of elements in Vb.net
[英]How to create multiple entries in an xml file in VB.Net?
單擊按鈕時,我需要將多個條目保存到xml文件中。 目前,它只保存一個條目,然后它將覆蓋下一個條目的xml文件,每個xml文件只允許一個條目。
Private Sub btnXmlSave_Click(sender As Object, e As EventArgs) Handles btnXmlSave.Click
Dim XmlSet As New XmlWriterSettings()
XmlSet.Indent = True
' Initialize the XmlWriter.
Dim XmlWrite As XmlWriter = XmlWriter.Create("MyCalc.xml", XmlSet)
With XmlWrite
' create the XML file
.WriteStartDocument()
.WriteComment("XML Database.")
.WriteStartElement("Data")
.WriteStartElement("Calculations")
' write the tags and the entry into the tags
.WriteStartElement("Number1")
.WriteString(txtNum1.Text.ToString())
.WriteEndElement()
.WriteStartElement("Number2")
.WriteString(txtNum2.Text.ToString())
.WriteEndElement()
.WriteStartElement("Operation")
.WriteString(txtResult.Text.ToString())
.WriteEndElement()
'Close entry
.WriteEndElement()
.WriteEndDocument()
.Close()
End With
' provide feedback to the user that the file was saved
MessageBox.Show("File 'MyCalc.xml' saved")
End Sub
預期的結果應該是這樣的(我添加到應用程序創建的XML代碼中):
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
<Calculations>
<Number1>34</Number1>
<Number2>2</Number2>
<Operation>34 - 2 = 32</Operation>
</Calculations>
<Calculations>
<Number1>3</Number1>
<Number2>2</Number2>
<Operation>3 - 2 = 1</Operation>
</Calculations>
</Data>
目前,代碼將使用第二個代碼覆蓋第一個(計算),因此代替我的程序顯示兩個操作,它只顯示一個。 我相信它可能是For Each循環,但我無法讓它工作。
再次感謝您提供的任何幫助!
以下是檢索xml數據的代碼:
Private Sub btnXmlRetrieve_Click(sender As Object, e As EventArgs) Handles btnXmlRetrieve.Click
Try
If IO.File.Exists("MyCalc.xml") Then
lstOutput.DataSource = Nothing
lstOutput.Items.Clear()
txtNum1.Clear()
txtNum2.Clear()
txtResult.Clear()
Dim xmlDoc As New XmlDocument()
Dim calcOrderNodes As XmlNodeList
Dim calcOrderNode As XmlNode
Dim num As Integer = 0
xmlDoc.Load("MyCalc.xml")
calcOrderNodes = xmlDoc.GetElementsByTagName("Calculations")
For Each calcOrderNode In calcOrderNodes
lstOutput.Items.Add(xmlDoc.GetElementsByTagName("Operation").Item(num).InnerText)
num = num + 1
Next
Else
MessageBox.Show("No operations were saved to a XML file.")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
編輯
所以,我能夠在應用程序中正確顯示它,但xml文件沒有正確組織。 我使用此代碼進行追加(這就是為什么它最初覆蓋與添加相比)。 如果不存在文件,則會創建正確的xml結構,但是一旦創建了文件,它就會保存,但是沒有正確形成。
新代碼(為長篇文章道歉,試圖修復它):
Private Sub btnXmlSave_Click(sender As Object, e As EventArgs) Handles btnXmlSave.Click
Try
If IO.File.Exists("MyCalc.xml") Then
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("MyCalc.xml")
Dim calc As XmlNode = xmlDoc.CreateElement("Calculations")
Dim num1 As XmlNode = xmlDoc.CreateElement("Number1")
Dim num2 As XmlNode = xmlDoc.CreateElement("Number2")
Dim Op As XmlNode = xmlDoc.CreateElement("Operation")
num1.InnerText = txtNum1.Text
num2.InnerText = txtNum2.Text
Op.InnerText = txtResult.Text
xmlDoc.LastChild.AppendChild(calc)
xmlDoc.LastChild.AppendChild(num1)
xmlDoc.LastChild.AppendChild(num2)
xmlDoc.LastChild.AppendChild(Op)
xmlDoc.Save("MyCalc.xml")
Else
Dim XmlSet As New XmlWriterSettings()
XmlSet.Indent = True
' Initialize the XmlWriter.
Dim XmlWrite As XmlWriter = XmlWriter.Create("MyCalc.xml", XmlSet)
With XmlWrite
' create the XML file
.WriteStartDocument()
.WriteComment("XML Database.")
.WriteStartElement("Data")
.WriteStartElement("Calculations")
' write the tags and the entry into the tags
.WriteStartElement("Number1")
.WriteString(txtNum1.Text.ToString())
.WriteEndElement()
.WriteStartElement("Number2")
.WriteString(txtNum2.Text.ToString())
.WriteEndElement()
.WriteStartElement("Operation")
.WriteString(txtResult.Text.ToString())
.WriteEndElement()
' close entry
.WriteEndElement()
.WriteEndDocument()
.Close()
End With
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
' provide feedback to the user that the file was saved
MessageBox.Show("File 'MyCalc.xml' saved")
End Sub
以及顯示如何保存的xml文件中的示例:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
<Calculations>
<Number1>2</Number1>
<Number2>1</Number2>
<Operation>2 + 1 = 3</Operation>
</Calculations>
<Calculations />
<Number1>3</Number1>
<Number2>2</Number2>
<Operation>3 / 2 = 1.50</Operation>
<Calculations />
<Number1>41</Number1>
<Number2>2</Number2>
<Operation>41 x 2 = 82</Operation>
</Data>
是的,使用for循環或每個應該得到你想要的。
例如
Private Sub btnXmlSave_Click(sender As Object, e As EventArgs) Handles btnXmlSave.Click
Dim XmlSet As New XmlWriterSettings()
XmlSet.Indent = True
' Initialize the XmlWriter.
Dim XmlWrite As XmlWriter = XmlWriter.Create("MyCalc.xml", XmlSet)
With XmlWrite
' create the XML file
.WriteStartDocument()
.WriteComment("XML Database.")
.WriteStartElement("Data")
For index As Integer = 1 To 2
.WriteStartElement("Calculations")
' write the tags and the entry into the tags
.WriteStartElement("Number1")
.WriteString(txtNum1.Text.ToString())
.WriteEndElement()
.WriteStartElement("Number2")
.WriteString(txtNum2.Text.ToString())
.WriteEndElement()
.WriteStartElement("Operation")
.WriteString(txtResult.Text.ToString())
.WriteEndElement()
'Close entry
.WriteEndElement()
Next
.WriteEndDocument()
.Close()
End With
' provide feedback to the user that the file was saved
MessageBox.Show("File 'MyCalc.xml' saved")
End Sub
更多信息可以在這里找到: https : //docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement
使用https://www.jdoodle.com/compile-vb-dot-net-online的工作示例 - 只需粘貼此代碼:
Imports System
Imports System.Xml
Public Class Test
Public Shared Sub Main()
Dim XmlSet As New XmlWriterSettings()
XmlSet.Indent = True
' Initialize the XmlWriter.
Dim XmlWrite As XmlWriter = XmlWriter.Create(Console.Out, XmlSet)
With XmlWrite
' create the XML file
.WriteStartDocument()
.WriteComment("XML Database.")
.WriteStartElement("Data")
For index As Integer = 1 To 2
.WriteStartElement("Calculations")
' write the tags and the entry into the tags
.WriteStartElement("Number1")
.WriteString("1")
.WriteEndElement()
.WriteStartElement("Number2")
.WriteString("2")
.WriteEndElement()
.WriteStartElement("Operation")
.WriteString("result")
.WriteEndElement()
'Close entry
.WriteEndElement()
Next
.WriteEndDocument()
.Close()
End With
' provide feedback to the user that the file was saved
console.Write(XmlWrite.ToString())
End Sub
End Class
嘗試這個:
If IO.File.Exists("MyCalc.xml") Then
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("MyCalc.xml")
Dim mainNode As XmlNode = xmlDoc.DocumentElement
Dim newOp As XmlNode = xmlDoc.CreateElement("Calculations")
'Create elements and append them to the main document node'
Dim subNode As XmlNode = xmlDoc.CreateElement("Number1")
subNode.InnerText = txtNum1.Text
newOp.AppendChild(subNode)
subNode = xmlDoc.CreateElement("Number2")
subNode.InnerText = txtNum2.Text
newOp.AppendChild(subNode)
subNode = xmlDoc.CreateElement("Operation")
subNode.InnerText = txtResult.Text
newOp.AppendChild(subNode)
'append child node to main node and save'
mainNode.AppendChild(newOp)
xmlDoc.Save("MyCalc.xml")
Else
'Add other code here'
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.