473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 5487
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
How do I read the attributes of this XML? I have a page with text boxes that i want to read these values in. notice there are 2 Parameter tags with the same attributes. Code would help ...
7
by: SQLScott | last post by:
I have a Web Service in which I am trying to pass an XMLDocument as a parameter to one of the methods. I would like to use the XMLTextReader to read the XML but I am getting the following error: ...
1
by: Andrew Jacobs | last post by:
I am using C# to write an application that migrates documents from one version of an XML schema/DTD to later one. Much of the document has the same structure in the two versions and I use...
4
by: David Grogan | last post by:
Hi, 2 questions.... 1. I'm parsing an XHTML document that contains both the default namespace (xmlns="http://www.w3.org/1999/xhtml") and a custom one (xmlns:r="...") - both of these being...
4
by: Jesper Stocholm | last post by:
I have a database class that maintains data about customers i my system. The basic XML for this looks like: <Chunk> <Vendor> <Database/> </Vendor> </Chunk> When a user is to be registrered...
1
by: David | last post by:
I need to find a good online resource which teaches the use of the XmlDocument framework in more depth than is covered in MS's online doc. I need to create a multi-level XML document like the one...
4
by: aaa | last post by:
Can someone show me a snippet that creates a very simple XML doc with either a root and chilc nodes or root child nodes, and attributes. I have looked all around and every example I try I get an...
3
by: Mungo Jerrie | last post by:
Hi there :-) I have some problems with the same instance of my xmldocument across 3 different classes. See code below: class one { protected static XmlDocument doc = new XmlDocument(); ...
1
by: =?Utf-8?B?RGF2aWRHQg==?= | last post by:
OK, so I've created and loaded an XMLDocument object. But how do I go about using it? Specifically, how do I: 1) move to the first node (I assume I start on it when I load the XML?) 2) move to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.