Using DOM, this is how I am appending data to an existing XML file
which is named AppendData.xml (the root element of AppendData.xml is
<ProductList> ):
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(Ser ver.MapPath("Ap pendData.xml"))
Dim eltProducts1 As XmlElement = xmlDoc.CreateEl ement("Products ")
Dim attProdID1 As XmlAttribute = xmlDoc.CreateAt tribute("ProdID 1")
eltProducts1.Se tAttributeNode( attProdID1)
eltProducts1.Se tAttribute("Pro dID1", "PID1")
Dim eltRoot1 As XmlElement = xmlDoc.Item("Pr oductList")
eltRoot1.Append Child(eltProduc ts1)
Dim eltName1 As XmlElement = xmlDoc.CreateEl ement("Name")
eltName1.InnerT ext = "Product1"
eltProducts1.Ap pendChild(eltNa me1)
Dim eltDescription1 As XmlElement =
xmlDoc.CreateEl ement("Descript ion")
eltDescription1 .InnerText = "Desc1"
eltProducts1.Ap pendChild(eltDe scription1)
Dim eltUnitPrice1 As XmlElement = xmlDoc.CreateEl ement("UnitPric e")
eltUnitPrice1.I nnerText = "250"
eltProducts1.Ap pendChild(eltUn itPrice1)
Dim eltProducts2 As XmlElement = xmlDoc.CreateEl ement("Products ")
Dim attProdID2 As XmlAttribute = xmlDoc.CreateAt tribute("ProdID 2")
eltProducts2.Se tAttributeNode( attProdID2)
eltProducts2.Se tAttribute("Pro dID2", "PID2")
Dim eltRoot2 As XmlElement = xmlDoc.Item("Pr oductList")
eltRoot2.Append Child(eltProduc ts2)
Dim eltName2 As XmlElement = xmlDoc.CreateEl ement("Name")
eltName2.InnerT ext = "Product2"
eltProducts2.Ap pendChild(eltNa me2)
Dim eltDescription2 As XmlElement =
xmlDoc.CreateEl ement("Descript ion")
eltDescription2 .InnerText = "Desc2"
eltProducts2.Ap pendChild(eltDe scription2)
Dim eltUnitPrice2 As XmlElement = xmlDoc.CreateEl ement("UnitPric e")
eltUnitPrice2.I nnerText = "200"
eltProducts2.Ap pendChild(eltUn itPrice2)
xmlDoc.Save(Ser ver.MapPath("Ap pendData.xml"))
End Sub
</script>
The above code successfully appends 2 sets of <Productsdata to the
AppendData.xml file which looks like this:
<ProductList>
<Products ProdID="PID1">
<Name>Product 1</Name>
<Description>De sc1</Description>
<UnitPrice>25 0</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product 2</Name>
<Description>De sc2</Description>
<UnitPrice>20 0</UnitPrice>
</Products>
</ProductList>
Note that in order to append 2 sets of <Productsdata to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Productsdata i.e. for the attribute ProdID, I used attProdID1 &
attProdID2, for the element Name, I used eltName1 & eltName2, for the
element Description, I used eltDescription1 & eltDescription2 & finally
for UnitPrice, I used eltUnitPrice1 & eltUnitPrice2. Had I used only 1
variable (say, attProdID for the attribute ProdID, eltName for the
element Name, eltDescription for the element Description & eltUnitPrice
for the element UnitPrice) to append the 2 sets of <Productsdata ,
only the 2nd set of <Productsdata would get appended to
AppendData.xml.
What I did like to know is - to append more than 1 set of <Products>
data to AppendData.xml, is there some other way out wherein using a
single variable for the attribute ProdID & elements Name, Description &
UnitPrice would create multiple sets of <Productsdata (by using
loops, maybe) instead of using different variables for the attribute
ProdID & elements Name, Description & UnitPrice to append each set of
<Productsdata (as shown in the ASPX code above)?
Thanks,
Arpan