Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to edit a value in root element of XML file

XML
<Model name="Test.cre" version="v2.0" unit="m" count="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">



Here Model is the root element I want to update the count attribute in root element using C#.

How to do this using C#



C#
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(@"C:\Users\KK\Desktop\Sample.xml");

XmlElement node1 = xmlDoc.SelectSingleNode("/root") as XmlElement;
           if (node1 != null)
           {

               node1.SetAttribute("count", nodeCount.ToString()); // if you want an attribute

           }

           xmlDoc.Save(@"C:\Users\KK\Desktop\Sample.xml");
Posted
Updated 16-Mar-14 22:37pm
v3
Comments
What have you tried and where is the problem?
KUMAR619 17-Mar-14 5:12am    
I have updated my code
Sampath Lokuge 17-Mar-14 5:20am    
What's the problem of your code snippet ? Is there any errors or what ?

1 solution

Please refer the following Link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900