如何在现有XML中添加父节点
我的程序使用vb.net语言,这是我的xml如何在现有XML中添加父节点,xml,vb.net,Xml,Vb.net,我的程序使用vb.net语言,这是我的xml <Result> <ID>1</ID> <VERSION_NO>1.0.0.1</VERSION_NO> <TIME>1</TIME> </Result> 1. 1.0.0.1 1. 如何添加父节点这个xml是这样的吗 <Result> <ID>1</ID> <VERSION_NO>1.0.0.1&
<Result>
<ID>1</ID>
<VERSION_NO>1.0.0.1</VERSION_NO>
<TIME>1</TIME>
</Result>
1.
1.0.0.1
1.
如何添加父节点这个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>
1.
1.0.0.1
1.
2.
1.0.0.2
5.
3.
1.0.0.3
5.
非常感谢。这是整个XML文档(不包括顶部的
)吗?因为如果是这样,您所要求的就不能直接完成——每个XML文档必须有一个根元素。
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