A
Arpan
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(Server.MapPath("AppendData.xml"))
Dim eltProducts1 As XmlElement = xmlDoc.CreateElement("Products")
Dim attProdID1 As XmlAttribute = xmlDoc.CreateAttribute("ProdID1")
eltProducts1.SetAttributeNode(attProdID1)
eltProducts1.SetAttribute("ProdID1", "PID1")
Dim eltRoot1 As XmlElement = xmlDoc.Item("ProductList")
eltRoot1.AppendChild(eltProducts1)
Dim eltName1 As XmlElement = xmlDoc.CreateElement("Name")
eltName1.InnerText = "Product1"
eltProducts1.AppendChild(eltName1)
Dim eltDescription1 As XmlElement =
xmlDoc.CreateElement("Description")
eltDescription1.InnerText = "Desc1"
eltProducts1.AppendChild(eltDescription1)
Dim eltUnitPrice1 As XmlElement = xmlDoc.CreateElement("UnitPrice")
eltUnitPrice1.InnerText = "250"
eltProducts1.AppendChild(eltUnitPrice1)
Dim eltProducts2 As XmlElement = xmlDoc.CreateElement("Products")
Dim attProdID2 As XmlAttribute = xmlDoc.CreateAttribute("ProdID2")
eltProducts2.SetAttributeNode(attProdID2)
eltProducts2.SetAttribute("ProdID2", "PID2")
Dim eltRoot2 As XmlElement = xmlDoc.Item("ProductList")
eltRoot2.AppendChild(eltProducts2)
Dim eltName2 As XmlElement = xmlDoc.CreateElement("Name")
eltName2.InnerText = "Product2"
eltProducts2.AppendChild(eltName2)
Dim eltDescription2 As XmlElement =
xmlDoc.CreateElement("Description")
eltDescription2.InnerText = "Desc2"
eltProducts2.AppendChild(eltDescription2)
Dim eltUnitPrice2 As XmlElement = xmlDoc.CreateElement("UnitPrice")
eltUnitPrice2.InnerText = "200"
eltProducts2.AppendChild(eltUnitPrice2)
xmlDoc.Save(Server.MapPath("AppendData.xml"))
End Sub
</script>
The above code successfully appends 2 sets of <Products> data to the
AppendData.xml file which looks like this:
<ProductList>
<Products ProdID="PID1">
<Name>Product1</Name>
<Description>Desc1</Description>
<UnitPrice>250</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product2</Name>
<Description>Desc2</Description>
<UnitPrice>200</UnitPrice>
</Products>
</ProductList>
Note that in order to append 2 sets of <Products> data to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Products> data 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 <Products> data,
only the 2nd set of <Products> data 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 <Products> data (by using
loops, maybe) instead of using different variables for the attribute
ProdID & elements Name, Description & UnitPrice to append each set of
<Products> data (as shown in the ASPX code above)?
Thanks,
Arpan
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(Server.MapPath("AppendData.xml"))
Dim eltProducts1 As XmlElement = xmlDoc.CreateElement("Products")
Dim attProdID1 As XmlAttribute = xmlDoc.CreateAttribute("ProdID1")
eltProducts1.SetAttributeNode(attProdID1)
eltProducts1.SetAttribute("ProdID1", "PID1")
Dim eltRoot1 As XmlElement = xmlDoc.Item("ProductList")
eltRoot1.AppendChild(eltProducts1)
Dim eltName1 As XmlElement = xmlDoc.CreateElement("Name")
eltName1.InnerText = "Product1"
eltProducts1.AppendChild(eltName1)
Dim eltDescription1 As XmlElement =
xmlDoc.CreateElement("Description")
eltDescription1.InnerText = "Desc1"
eltProducts1.AppendChild(eltDescription1)
Dim eltUnitPrice1 As XmlElement = xmlDoc.CreateElement("UnitPrice")
eltUnitPrice1.InnerText = "250"
eltProducts1.AppendChild(eltUnitPrice1)
Dim eltProducts2 As XmlElement = xmlDoc.CreateElement("Products")
Dim attProdID2 As XmlAttribute = xmlDoc.CreateAttribute("ProdID2")
eltProducts2.SetAttributeNode(attProdID2)
eltProducts2.SetAttribute("ProdID2", "PID2")
Dim eltRoot2 As XmlElement = xmlDoc.Item("ProductList")
eltRoot2.AppendChild(eltProducts2)
Dim eltName2 As XmlElement = xmlDoc.CreateElement("Name")
eltName2.InnerText = "Product2"
eltProducts2.AppendChild(eltName2)
Dim eltDescription2 As XmlElement =
xmlDoc.CreateElement("Description")
eltDescription2.InnerText = "Desc2"
eltProducts2.AppendChild(eltDescription2)
Dim eltUnitPrice2 As XmlElement = xmlDoc.CreateElement("UnitPrice")
eltUnitPrice2.InnerText = "200"
eltProducts2.AppendChild(eltUnitPrice2)
xmlDoc.Save(Server.MapPath("AppendData.xml"))
End Sub
</script>
The above code successfully appends 2 sets of <Products> data to the
AppendData.xml file which looks like this:
<ProductList>
<Products ProdID="PID1">
<Name>Product1</Name>
<Description>Desc1</Description>
<UnitPrice>250</UnitPrice>
</Products>
<Products ProdID="PID2">
<Name>Product2</Name>
<Description>Desc2</Description>
<UnitPrice>200</UnitPrice>
</Products>
</ProductList>
Note that in order to append 2 sets of <Products> data to
AppendData.xml, I have used 2 different variables to append the 2 sets
of <Products> data 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 <Products> data,
only the 2nd set of <Products> data 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 <Products> data (by using
loops, maybe) instead of using different variables for the attribute
ProdID & elements Name, Description & UnitPrice to append each set of
<Products> data (as shown in the ASPX code above)?
Thanks,
Arpan