DOM CreateElement?

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
 
M

Martin Honnen

Arpan wrote:

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.

I don't see any problem using one variable for each element type as long
as you create each element as needed and insert it e.g.

Dim Products As XmlElement
Dim Name As XmlElement
Dim Description As XmlElement
Dim UnitPrice As XmlElement

Products = xmlDoc.CreateElement("Products")
Products.SetAttribute("ProdID", "PID1")

Name = xmlDoc.CreateElement("Name")
Name.InnerText = "Product1"

Products.AppendChild(Name)

Description = xmlDoc.CreateElement("Description")
Description.InnerText = "Desc1"

Products.AppendChild(Description)

UnitPrice = xmlDoc.CreateElement("UnitPrice")
UnitPrice.InnerText = "250"

Products.AppendChild(UnitPrice)

xmlDoc.DocumentElement.AppendChild(Products)

Products = xmlDoc.CreateElement("Products")
Products.SetAttribute("ProdID", "PID2")

Name = xmlDoc.CreateElement("Name")
Name.InnerText = "Product2"

Products.AppendChild(Name)

Description = xmlDoc.CreateElement("Description")
Description.InnerText = "Desc2"

Products.AppendChild(Description)

UnitPrice = xmlDoc.CreateElement("UnitPrice")
UnitPrice.InnerText = "200"

Products.AppendChild(UnitPrice)

xmlDoc.DocumentElement.AppendChild(Products)

xmlDoc.Save(Server.MapPath("file.xml"))

There is also no need to create attribute nodes if you simply use the
SetAttribute method on an XmlElement node anyway.
 
A

Arpan

Thanks Martin, your suggestions have really been very very helpful.
After going through your code, I realized that retrieving the root
element <ProductList> using

Dim eltRoot1 As XmlElement = xmlDoc.Item("ProductList"))

(as shown in the code in post #1) isn't necessary as well.

One last question please - what's the difference between a Node & an
Element in XML? Or are they one & the same?

Thanks once again,

Regards,

Arpan
 
M

Martin Honnen

Arpan wrote:

One last question please - what's the difference between a Node & an
Element in XML? Or are they one & the same?

There are many different views and models and specifications. The XML
specification itself <http://www.w3.org/TR/REC-xml/> does not talk about
nodes at all but object models do.
In the DOM data model and in the XPath data model there is a base type,
the node, and there are specializations of the node type.
So any element in the DOM is a node, an element node, but there are
other types of nodes, for instance text nodes, attribute nodes,
processing instruction nodes, comment nodes, the document node.
In the .NET DOM object model/class hierarchy that is reflected, there is
an abstract base class XmlNode from which several other node types are
derived
<http://msdn.microsoft.com/library/d.../html/frlrfsystemxmlxmlnodeclasshierarchy.asp>
<http://msdn.microsoft.com/library/d...frlrfsystemxmlxmllinkednodeclasshierarchy.asp>

As said, the XPath data model also knows nodes in general and
specializations of that general node type, like element nodes, attribute
nodes, text nodes, comment nodes, processing instruction nodes. There
are however some differences between how XPath models certain things in
an XML document and how the DOM models the same things. For instance XML
namespace declarations you find in XML document with namespace e.g.
<element xmlns="http://example.com/ns1">
or
<element xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="http://example.org/2006/ex1">
are simply modelled as attribute nodes in the DOM while the XPath data
model does not have those namespace declarations as attributes but
rather introduces special namespace nodes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top