473,811 Members | 3,485 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 5511
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
4708
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
4907
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
4340
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
4183
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
18189
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
1613
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
15988
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
1870
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
1707
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
9730
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10392
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...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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
7671
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
5555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
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.