Mega Code Archive

 
Categories / C# Tutorial / XML
 

The following example adds an attribute to an element

using System; using System.IO; using System.Xml; public class Sample {   public static void Main()   {     XmlDocument doc = new XmlDocument();     doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='11111111111'><title>A</title></book>");     XmlElement root = doc.DocumentElement;     XmlAttribute attr = root.SetAttributeNode("genre", "urn:samples");     attr.Value="novel";     Console.WriteLine(doc.InnerXml);   } }