469,926 Members | 2,412 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

XMLDocument - Appending Attributes to Various Element Tags

I'm using the followg code to add the attribute overwrite='true" to a select list of XML tags in an XML document. The document is loaded from a file and just the tags with names matching what's in the ArrayList are updated to contain the new attribute. However, after it saves back to the XML file, I find that only the last one in the list was updated and all the ones prior to the last one in the loop are skipped. Why is it losing the updates before the last one? How can I get it to add the attribute to all the tags in the list? Thanks.
--------------------------------------------------------------------------------
Dim Tg As OverwritableTag
Dim XD As XmlDocument
Dim XNL As XmlNodeList
Dim XN As XmlNode
Dim XA As XmlAttribute

If alOVR.Count 0 Then
XD = New XmlDocument()
XD.Load(mXmlPath)
XA = XD.CreateAttribute("overwrite")
XA.Value = "true"
For Each Tg In alOVR
XNL = XD.GetElementsByTagName("ml:" & [Enum].GetName(GetType(OverwritableTag), Tg))
For Each XN In XNL
XN.Attributes.Append(XA)
Next
Next
XD.Save(mXmlPath)
XD = Nothing
End If
Nov 20 '06 #1
2 5333
Phil Galey wrote:
I'm using the followg code to add the attribute *overwrite='true"* to a
select list of XML tags in an XML document. The document is loaded from
a file and just the tags with names matching what's in the ArrayList are
updated to contain the new attribute. However, after it saves back to
the XML file, I find that only the last one in the list was updated and
all the ones prior to the last one in the loop are skipped. Why is it
losing the updates before the last one? How can I get it to add the
attribute to all the tags in the list?
Use CreateAttribute inside of the for each loop to create a new
attribute node for each element or simply use SetAttribute e.g.

For Each XN In XNL
XN.SetAttribute("overwrite", "true")

that suffices, no need to use CreateAttribute to set an attribute.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 21 '06 #2
On Mon, 20 Nov 2006 14:50:53 -0800, Phil Galey wrote:
I'm using the followg code to add the attribute overwrite='true" to a select list of XML tags in an XML document. The document is loaded from a file and just the tags with names matching what's in the ArrayList are updated to contain the new attribute. However, after it saves back to the XML file, I find that only the last one in the list was updated and all the ones prior to the last one in the loop are skipped. Why is it losing the updates before the last one? How can I get it to add the attribute to all the tags in the list? Thanks.
--------------------------------------------------------------------------------
Dim Tg As OverwritableTag
Dim XD As XmlDocument
Dim XNL As XmlNodeList
Dim XN As XmlNode
Dim XA As XmlAttribute

If alOVR.Count 0 Then
XD = New XmlDocument()
XD.Load(mXmlPath)
XA = XD.CreateAttribute("overwrite")
XA.Value = "true"
For Each Tg In alOVR
XNL = XD.GetElementsByTagName("ml:" & [Enum].GetName(GetType(OverwritableTag), Tg))
For Each XN In XNL
XN.Attributes.Append(XA)
Next
Next
XD.Save(mXmlPath)
XD = Nothing
End If
Don't use XmlDocument for iterating through tags. Just open an
XmlTextReader and XmlTextWriter, then iterate over the contents adding the
attribute when you need it.
XmlDocument loads the entire document into RAM by parsing it and building
an object tree, which sounds like a big uneccesary overhead for what you're
trying to do.
XmlTextReader is relatively easy to use and much much faster than
XmlDocument.

Cheers,
Gadget
Nov 23 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by | last post: by
1 post views Thread by Andrew Jacobs | last post: by
4 posts views Thread by Jesper Stocholm | last post: by
4 posts views Thread by aaa | last post: by
3 posts views Thread by Mungo Jerrie | last post: by
1 post views Thread by =?Utf-8?B?RGF2aWRHQg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.