473,769 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Updating existing XML attribute

This should be easy, and it probably is, I just can't find anything on it.

I have to build an XML Document object in memory, add some child nodes, save
it, and then if another condition arises, reload the same xml document and
append some more child nodes. And by the way, the user requires that I save
between all these occurances so I can't just leave it in memory and then save
it just once.

So far everything works exactly as I need it to with one exception. There
is an attribute on the Root that has a count of each of the child nodes. I
get it set to an original value of 0, but have so far been unable to figure
out how to get it updated. Here's the relevent code:

if (cwaCount == 0)
{
// Write the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXm lDeclaration("1 .0",
"utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateEl ement("cwa_bloc ks");
xmlDoc.InsertBe fore(xmlDeclara tion, xmlDoc.Document Element);
rootNode.SetAtt ribute("Count", cwaCount.ToStri ng());
xmlDoc.AppendCh ild(rootNode);
}
else
{
xmlDoc.Load(eve ntDir + @"\cwa.xml") ;
}
// Create a new <cwaelement and add it to the root node
XmlElement parentNode = xmlDoc.CreateEl ement("cwa");
xmlDoc.Document Element.Prepend Child(parentNod e);
//OTHER PROCESSING TO ADD CHILD NODES THAT WORKS

//Save file
xmlDoc.Save(eve ntDir + @"\cwa.xml") ;

cwaCount++;
XmlElement root = xmlDoc.Document Element;
XmlAttribute attr = root.GetAttribu teNode("Count") ;

//OBVIOUSLY SOMETHING WRONG HERE...THE attr NEVER UPDATES
attr.InnerXml = cwaCount.ToStri ng();

I've tried updating a number of ways, but none have worked so far.
Any help greatly appreciated.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
Aug 23 '06 #1
2 2075


WhiteWizard wrote:

cwaCount++;
XmlElement root = xmlDoc.Document Element;
XmlAttribute attr = root.GetAttribu teNode("Count") ;

//OBVIOUSLY SOMETHING WRONG HERE...THE attr NEVER UPDATES
attr.InnerXml = cwaCount.ToStri ng();
xmlDoc.Document Element.SetAttr ibute("Count", cwaCount.ToStri ng());
should suffice, much the same as you did some lines before. No need to
first access the attribute as a node.
If you have an XmlAttribute instance then to change its value you do e.g.
attr.Value = cwaCount.ToStri ng();
but usually calling SetAttribute on the element suffices.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 23 '06 #2
NEVER MIND!!!!

I'm having a senior moment.

I just realized when looking over my posting that the update was AFTER the
save. In my defense I'm writing to a remote memory module AND a local hard
drive, and didn't notice that I was already past the RMM save.

Just a VERY long week.
--
MCSD.NET, MCAD, MCT
"WhiteWizar d" wrote:
This should be easy, and it probably is, I just can't find anything on it.

I have to build an XML Document object in memory, add some child nodes, save
it, and then if another condition arises, reload the same xml document and
append some more child nodes. And by the way, the user requires that I save
between all these occurances so I can't just leave it in memory and then save
it just once.

So far everything works exactly as I need it to with one exception. There
is an attribute on the Root that has a count of each of the child nodes. I
get it set to an original value of 0, but have so far been unable to figure
out how to get it updated. Here's the relevent code:

if (cwaCount == 0)
{
// Write the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXm lDeclaration("1 .0",
"utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateEl ement("cwa_bloc ks");
xmlDoc.InsertBe fore(xmlDeclara tion, xmlDoc.Document Element);
rootNode.SetAtt ribute("Count", cwaCount.ToStri ng());
xmlDoc.AppendCh ild(rootNode);
}
else
{
xmlDoc.Load(eve ntDir + @"\cwa.xml") ;
}
// Create a new <cwaelement and add it to the root node
XmlElement parentNode = xmlDoc.CreateEl ement("cwa");
xmlDoc.Document Element.Prepend Child(parentNod e);
//OTHER PROCESSING TO ADD CHILD NODES THAT WORKS

//Save file
xmlDoc.Save(eve ntDir + @"\cwa.xml") ;

cwaCount++;
XmlElement root = xmlDoc.Document Element;
XmlAttribute attr = root.GetAttribu teNode("Count") ;

//OBVIOUSLY SOMETHING WRONG HERE...THE attr NEVER UPDATES
attr.InnerXml = cwaCount.ToStri ng();

I've tried updating a number of ways, but none have worked so far.
Any help greatly appreciated.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
Aug 23 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6594
by: Frostillicus | last post by:
Hi, I've written some javascript to randomly choose a classical music composer's picture and sample audio and display it on my home page (http://marc.fearby.com/), and this works fine in Mozilla but has problems in IE. Here's the code: imagestring = "<img src=\"" + imagedir + composers + "\" />"; objimage.innerHTML = imagestring;
0
1767
by: | last post by:
I am updating MS access tables with data in an xml document. I create two dataset, one for existing data and one for new data. I fill the first dataset with the records from MS Access, the second with data from the xml Document. Both datasets are populated properly. When the new data contains records that exist in the existing Access table but with changes in individual elements, the matching records in the first dataset are not changed...
4
2414
by: Winshent | last post by:
I am having problems with adding items to my shopping cart. The problem occures when adding items that already exists in the cart. When a user adds to cart, they are automatically redirected to the cart, where they can update quantities. If the item already exists in the cart, i want to update the quantity by adding 1 to the quantity of that item. The code all works fine when i step thru the code, however when running it without...
9
1745
by: Rocky | last post by:
I have 2 textboxes. When I click submit, i want to add whatevers in the text box1 as username and whatevers in textbox2 as userid into an xml file. How do I do that in ASP.NET using vb.net? My xml file look like this: <?xml version="1.0" encoding="utf-8" ?> <!-- format is <user>userid</user> --> <userdata> <user name="Username1"> <userid>abc1</userid>
34
10844
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have VBA code that wrote the results of that away to the db, either creating new records or updating existing records, whichever was relevant. This may also include deleting records. Now I generally do this by opening a recordset on the source data...
1
1950
by: Tedros.G | last post by:
Hi I have an attribute the appears in both the root node and child node for example, below the attribute VERSION appears in the rood node (PRODMSG ) and a child node (OPERATION ) ================ INPUT XML ================ <?xml version="1.0" encoding="utf-16"?> <PRODMSG VERSION="1.2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12
1363
lotus18
by: lotus18 | last post by:
Hi everyone I have a little problem in updating a record. I know how add a new record and check if the inputted new data is existing or not as what they have discussed in http://www.thescripts.com/forum/thread733529.html. Now, my problem is... How can I check if updating data is existing or not? Please help!!! Here's my sample code in adding a new entry: Public dbConnection As New ADODB.Connection Dim dbPath As String
5
5353
by: rosaryshop | last post by:
I'm working a jewelry/rosary design web site at http://www.rosaryshop.com/rosariesAndKits2.php. As the user makes selections, it updates images of various parts, giving them a preview of the finished item. The preview resides within a div and is simply a series of <br>-separated images. Simple html. The system is working fine with FireFox and Safari, but some MSIE 6 and 7 users are reporting that only the first image is updating and the...
1
165
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and re-run.... fun learning with python... Edwin -----Original Message----- From: Madari, Edwin Sent: Thursday, August 14, 2008 9:24 PM
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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
10051
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
10000
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
9866
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
8879
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...
0
6675
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
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.