Cómo cambiar el atributo XML

¿Cómo puedo cambiar un atributo de un elemento en un archivo XML, usando C #?

Micro; Cada vez que necesito modificar un documento XML lo trabajo de esta manera:

//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 

Espero que le sea útil

Usando LINQ to xml si está utilizando 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"); 

Si el atributo que desea cambiar no existe o se ha eliminado accidentalmente, se produce una excepción. Le sugiero que primero cree un nuevo atributo y lo envíe a una función como la siguiente:

 private void SetAttrSafe(XmlNode node,params XmlAttribute[] attrList) { foreach (var attr in attrList) { if (node.Attributes[attr.Name] != null) { node.Attributes[attr.Name].Value = attr.Value; } else { node.Attributes.Append(attr); } } } 

Uso:

  XmlAttribute attr = dom.CreateAttribute("name"); attr.Value = value; SetAttrSafe(node, attr); 

Aquí está el comienzo de una clase de analizador para que comiences. Esto terminó siendo mi solución a un problema similar:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace XML { public class Parser { private string _FilePath = string.Empty; private XDocument _XML_Doc = null; public Parser(string filePath) { _FilePath = filePath; _XML_Doc = XDocument.Load(_FilePath); } ///  /// Replaces values of all attributes of a given name (attributeName) with the specified new value (newValue) in all elements. ///  ///  ///  public void ReplaceAtrribute(string attributeName, string newValue) { ReplaceAtrribute(string.Empty, attributeName, new List { }, newValue); } ///  /// Replaces values of all attributes of a given name (attributeName) with the specified new value (newValue) in elements with a given name (elementName). ///  ///  ///  ///  public void ReplaceAtrribute(string elementName, string attributeName, string newValue) { ReplaceAtrribute(elementName, attributeName, new List { }, newValue); } ///  /// Replaces values of all attributes of a given name (attributeName) and value (oldValue) /// with the specified new value (newValue) in elements with a given name (elementName). ///  ///  ///  ///  ///  public void ReplaceAtrribute(string elementName, string attributeName, string oldValue, string newValue) { ReplaceAtrribute(elementName, attributeName, new List { oldValue }, newValue); } ///  /// Replaces values of all attributes of a given name (attributeName), which has one of a list of values (oldValues), /// with the specified new value (newValue) in elements with a given name (elementName). /// If oldValues is empty then oldValues will be ignored. ///  ///  ///  ///  ///  public void ReplaceAtrribute(string elementName, string attributeName, List oldValues, string newValue) { List elements = _XML_Doc.Elements().Descendants().ToList(); foreach (XElement element in elements) { if (elementName == string.Empty | element.Name.LocalName.ToString() == elementName) { if (element.Attribute(attributeName) != null) { if (oldValues.Count == 0 || oldValues.Contains(element.Attribute(attributeName).Value)) { element.Attribute(attributeName).Value = newValue; } } } } } public void SaveChangesToFile() { _XML_Doc.Save(_FilePath); } } }