Mega Code Archive

 
Categories / C# Tutorial / XML
 

Add attribute to Xml document

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml;    public class MainClass    {       public static void Main()       {          XmlDocument document = new XmlDocument();          document.Load("Books.xml");          XmlElement root = document.DocumentElement;          XmlElement newBook = document.CreateElement("book");          XmlElement newTitle = document.CreateElement("title");          XmlElement newAuthor = document.CreateElement("author");          XmlElement newCode = document.CreateElement("code");          XmlText title = document.CreateTextNode("C#");          XmlText author = document.CreateTextNode("A");          XmlText code = document.CreateTextNode("1234567890");          XmlComment comment = document.CreateComment("book");          XmlAttribute newPages = document.CreateAttribute("Pages");          newPages.Value = "1000";          newBook.Attributes.Append(newPages);          newBook.AppendChild(comment);          newBook.AppendChild(newTitle);          newBook.AppendChild(newAuthor);          newBook.AppendChild(newCode);          newTitle.AppendChild(title);          newAuthor.AppendChild(author);          newCode.AppendChild(code);          root.InsertAfter(newBook, root.FirstChild);          document.Save("Books.xml");       }    }