0
一個新條目下面我有一個這樣的XML,我想在另一個條目添加到它:插入XML
<?xml version="1.0" encoding="utf-8"?>
<CampaignRewardsVoucher>
<VoucherCode>Vouch001</VoucherCode>
<Quantity>3</Quantity>
</CampaignRewardsVoucher>
我有上面的XML,但我想Vouch001後添加Vouch002:
<VoucherCode>Vouch002</VoucherCode>
<Quantity>3</Quantity>
</CampaignRewardsVoucher>
下面我有它檢查如果輸入一個重複,並相應地更新代碼,如果不是我想創建一個新的Vouch002項,請指點,謝謝:
'Create XmlWriterSettings
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
If (Not File.Exists("D:\CampaignRewardsVoucher.xml")) Then
'Create XmlWriter
Using writer As XmlWriter = XmlWriter.Create("D:\CampaignRewardsVoucher.xml", settings)
'Begin write
writer.WriteStartDocument()
writer.WriteStartElement("CampaignRewardsVoucher")
writer.WriteElementString("VoucherCode", DropDownList1.SelectedValue)
writer.WriteElementString("Quantity", TextBox1.Text)
'End write
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
Else
' file already exist, next check if input data already exist
Dim myXmlDocument As XmlDocument = New XmlDocument()
myXmlDocument.Load("D:\CampaignRewardsVoucher.xml")
Dim myXMLNode As XmlNode = myXmlDocument.SelectSingleNode("CampaignRewardsVoucher")
If myXMLNode IsNot Nothing And myXMLNode.ChildNodes(0).InnerText = DropDownList1.SelectedValue Then
myXMLNode.ChildNodes(1).InnerText = TextBox1.Text
myXmlDocument.Save("D:\CampaignRewardsVoucher.xml")
Else
'insert new node
'I need to insert Vouch002 here.
End If
End If
的SelectSingleNode總會選擇第一CampaignRewardsVoucher所需的位置添加一個新的子節點。它不會看別人。這將導致XML中的數據重複 – evpo