473,398 Members | 2,427 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,398 software developers and data experts.

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.CreateXmlDeclaration("1.0",
"utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("cwa_blocks");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
rootNode.SetAttribute("Count", cwaCount.ToString());
xmlDoc.AppendChild(rootNode);
}
else
{
xmlDoc.Load(eventDir + @"\cwa.xml");
}
// Create a new <cwaelement and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("cwa");
xmlDoc.DocumentElement.PrependChild(parentNode);
//OTHER PROCESSING TO ADD CHILD NODES THAT WORKS

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

cwaCount++;
XmlElement root = xmlDoc.DocumentElement;
XmlAttribute attr = root.GetAttributeNode("Count");

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

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 2060


WhiteWizard wrote:

cwaCount++;
XmlElement root = xmlDoc.DocumentElement;
XmlAttribute attr = root.GetAttributeNode("Count");

//OBVIOUSLY SOMETHING WRONG HERE...THE attr NEVER UPDATES
attr.InnerXml = cwaCount.ToString();
xmlDoc.DocumentElement.SetAttribute("Count", cwaCount.ToString());
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.ToString();
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
"WhiteWizard" 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.CreateXmlDeclaration("1.0",
"utf-8", null);

// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("cwa_blocks");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
rootNode.SetAttribute("Count", cwaCount.ToString());
xmlDoc.AppendChild(rootNode);
}
else
{
xmlDoc.Load(eventDir + @"\cwa.xml");
}
// Create a new <cwaelement and add it to the root node
XmlElement parentNode = xmlDoc.CreateElement("cwa");
xmlDoc.DocumentElement.PrependChild(parentNode);
//OTHER PROCESSING TO ADD CHILD NODES THAT WORKS

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

cwaCount++;
XmlElement root = xmlDoc.DocumentElement;
XmlAttribute attr = root.GetAttributeNode("Count");

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

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
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...
0
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...
4
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...
9
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...
34
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...
1
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 ) ================...
12
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...
5
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...
1
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...
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
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
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,...
0
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...
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...
0
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...

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.