473,605 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(mXmlPat h)
XA = XD.CreateAttrib ute("overwrite" )
XA.Value = "true"
For Each Tg In alOVR
XNL = XD.GetElementsB yTagName("ml:" & [Enum].GetName(GetTyp e(OverwritableT ag), Tg))
For Each XN In XNL
XN.Attributes.A ppend(XA)
Next
Next
XD.Save(mXmlPat h)
XD = Nothing
End If
Nov 20 '06 #1
2 5500
Phil Galey wrote:
I'm using the followg code to add the attribute *overwrite='tru e"* 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(mXmlPat h)
XA = XD.CreateAttrib ute("overwrite" )
XA.Value = "true"
For Each Tg In alOVR
XNL = XD.GetElementsB yTagName("ml:" & [Enum].GetName(GetTyp e(OverwritableT ag), Tg))
For Each XN In XNL
XN.Attributes.A ppend(XA)
Next
Next
XD.Save(mXmlPat h)
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
4702
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 <TranslationRecords> <TranslationRecord TrxID="1">
7
4887
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: Value of type System.xml.xmldocument cannot be converted to System.IO.textreader. I would think this is possible to do. Code snippet is below:
1
4328
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 XMLDocument.ImportNode to copy unchanged sub-trees between the old and new document but this takes much longer than it should. I profiled the code and found that when ImportNode copies an element it initialises any default attributes defined for the...
4
4173
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 attributes of <html>. When I then insert new XmlNodes into the DOM the HTML tags all end up getting an unnecesarry 'xmlns' attribute added, elements with the 'r' prefix don't, which is correct. Is there any way to stop the XmlNode/XmlWriter from...
4
18171
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 in the system, XML like this is created
1
1608
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 listed below. Using XmlDocument, I can create the <books> and the individual <name> and <price> elements, but I am unsure how to create the <category> and <title> elements and close them after creating the detail elements. Eg. how can I go 2+...
4
15977
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 error of either: 1. Cannot set a value on node type: Element. 2. Specified argument was out of the range of valid values. 3. This document already has a DocumentElement node. Here is the code i've been working with:
3
1859
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(); public static XmlDocument getXmlDocument(){
1
1698
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 the next node (.read?) 3) move back to a previous node? Is there a sample program somewhere to show this? (preferably VB.net) TIA David
0
8423
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8411
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8281
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5885
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5444
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2437
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1270
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.