How to change XML Attribute

Mike picture Mike · Dec 15, 2008 · Viewed 127.2k times · Source

How can I change an attribute of an element in an XML file, using C#?

Answer

El Padrino picture El Padrino · Dec 15, 2008

Mike; Everytime I need to modify an XML document I work it this way:

//Here is the variable with which you assign a new value to the attribute
string newValue = string.Empty;
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(xmlFile);

XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
node.Attributes[0].Value = newValue;

xmlDoc.Save(xmlFile);

//xmlFile is the path of your file to be modified

I hope you find it useful