code_junkie

how to change xml value file using c#

Nov 12th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. //Here is the variable with which you assign a new value to the attribute
  2. string newValue = string.Empty
  3. XmlDocument xmlDoc = new XmlDocument();
  4.  
  5. xmlDoc.Load(xmlFile);
  6.  
  7. XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
  8. node.Attributes[0].Value = newValue;
  9.  
  10. xmlDoc.Save(xmlFile);
  11.  
  12. //xmlFile is the path of your file to be modified
  13.  
  14. using System.Xml.Linq;
  15.  
  16. XDocument xmlFile = XDocument.Load("books.xml");
  17.  
  18. var query = from c in xmlFile.Elements("catalog").Elements("book")
  19.  
  20. select c;
  21.  
  22. foreach (XElement book in query)
  23.  
  24. {
  25.  
  26. book.Attribute("attr1").Value = "MyNewValue";
  27.  
  28. }
  29.  
  30. xmlFile.Save("books.xml");
Add Comment
Please, Sign In to add comment