Hi,
How can I change one value (attribute) in xml file, using C#/
Thanks,
Hi,
How can I change one value (attribute) in xml file, using C#/
Thanks,
Using LINQ to xml if you are using framework 3.5:
using System.Xml.Linq;
XDocument xmlFile = XDocument.Load("books.xml");
var query = from c in xmlFile.Elements("catalog").Elements("book")
select c;
foreach (XElement book in query)
{
book.Attribute("attr1").Value = "MyNewValue";
}
xmlFile.Save("books.xml");
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