I have a xml file and I want to modified/append it by adding some nodes. I have written a below code
我有一个xml文件,我想通过添加一些节点来修改/追加它。我写了一个下面的代码
its giving an error -
它给出了一个错误 -
Cannot insert a node or any ancestor of that node as a child of itself.
无法将该节点的节点或任何祖先插入其自身的子节点。
Code-
Dim xdoc1 As New XmlDocument()
xdoc1.Load("C:\xml1.xml1")
Dim TrialNo As XmlNode = xdoc1.CreateElement("TrialNo")
Dim TotalNoOfTrials = GetTotalNoOfTrials()
TrialNo.InnerText = TotalNoOfTrials + 1
TrialNo.AppendChild(name1)
xdoc1.Save(Path)
I have below existing file
我有以下现有文件
<?xml version="1.0" encoding="utf-8"?>
<Data>
<TrialNo>1</TrialNo>
</Data>
and I want to append file like
我想附加文件
<?xml version="1.0" encoding="utf-8"?>
<Data>
<TrialNo>1</TrialNo>
<TrialNo>2</TrialNo>
<TrialNo>3</TrialNo>
</Data>
1 个解决方案
#1
0
Select root element which is Data here and then append two childs i.e. TrialNo.
选择此处为Data的根元素,然后追加两个子元素,即TrialNo。
Steps:
'Open xml file
Dim xdoc As New XmlDocument
xdoc.Load("Xml1.xml")
'Select root node
Dim xnode As XmlNode = xdoc.SelectSingleNode("Data")
'Create child element TrialNo
Dim childone As XmlElement = xdoc.CreateElement("TrialNo")
childone.InnerText = "2"
xnode.AppendChild(childone)
'Create child element TrialNo
Dim childtwo As XmlElement = xdoc.CreateElement("TrialNo")
childtwo.InnerText = "3"
xnode.AppendChild(childtwo)
'Save the entries
xdoc.Save("Xml1.xml")
#1
0
Select root element which is Data here and then append two childs i.e. TrialNo.
选择此处为Data的根元素,然后追加两个子元素,即TrialNo。
Steps:
'Open xml file
Dim xdoc As New XmlDocument
xdoc.Load("Xml1.xml")
'Select root node
Dim xnode As XmlNode = xdoc.SelectSingleNode("Data")
'Create child element TrialNo
Dim childone As XmlElement = xdoc.CreateElement("TrialNo")
childone.InnerText = "2"
xnode.AppendChild(childone)
'Create child element TrialNo
Dim childtwo As XmlElement = xdoc.CreateElement("TrialNo")
childtwo.InnerText = "3"
xnode.AppendChild(childtwo)
'Save the entries
xdoc.Save("Xml1.xml")