我的程序使用的是vb.net语言,这是我的xml
<Result>
<ID>1</ID>
<VERSION_NO>1.0.0.1</VERSION_NO>
<TIME>1</TIME>
</Result>
如何添加父节点就像这样的xml
<Result>
<ID>1</ID>
<VERSION_NO>1.0.0.1</VERSION_NO>
<TIME>1</TIME>
</Result>
<Result>
<ID>2</ID>
<VERSION_NO>1.0.0.2</VERSION_NO>
<TIME>5</TIME>
</Result>
<Result>
<ID>3</ID>
<VERSION_NO>1.0.0.3</VERSION_NO>
<TIME>5</TIME>
</Result>
非常感谢。
答案 0 :(得分:0)
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFilename As String = "C:\Junk\Junk.xml"
'Create the original file'
Dim xdc As New XmlDocument
xdc.AppendChild(xdc.CreateElement("Results")) 'This is the document node that contains all other nodes'
xdc.FirstChild.AppendChild(NewResult(xdc, "1", "1.0.0.1", "1"))
xdc.Save(strFilename) 'save the file with only one Result node'
xdc = Nothing
'now load the file and add two more nodes'
Dim xdc2 As New XmlDocument
xdc2.Load(strFilename)
xdc2.FirstChild.AppendChild(NewResult(xdc2, "2", "1.0.0.2", "5"))
xdc2.FirstChild.AppendChild(NewResult(xdc2, "3", "1.0.0.3", "5"))
xdc2.Save(strFilename) 'save the file with the new nodes added'
xdc2 = Nothing
'now display the file'
Dim s As String = My.Computer.FileSystem.ReadAllText(strFilename)
MsgBox(s)
End Sub
Private Shared Function NewResult(xdc As XmlDocument, id As String, versionNo As String, time As String) As XmlNode
Dim xndResult As XmlNode = xdc.CreateElement("Result")
Dim xndID As XmlNode = xdc.CreateElement("ID")
xndID.AppendChild(xdc.CreateTextNode(id))
xndResult.AppendChild(xndID)
Dim xndVersionNo As XmlNode = xdc.CreateElement("VERSION_NO")
xndVersionNo.AppendChild(xdc.CreateTextNode(versionNo))
xndResult.AppendChild(xndVersionNo)
Dim xndTime As XmlNode = xdc.CreateElement("TIME")
xndTime.AppendChild(xdc.CreateTextNode(time))
xndResult.AppendChild(xndTime)
Return xndResult
End Function
End Class