Add attribute to Xml document : XmlDocument « XML « C# / CSharp Tutorial






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");
      }
   }








30.8.XmlDocument
30.8.1.XmlDocument Event: node changed, node inserted and node removed
30.8.2.Add attribute to Xml document
30.8.3.Read Write Xml
30.8.4.Remove child from XmlDocument
30.8.5.Get child count
30.8.6.Get element by tag name
30.8.7.Get value from selected node
30.8.8.Loop Through XmlDocument Recursively
30.8.9.Select Root
30.8.10.Three ways to load Xml to XmlDocument
30.8.11.Fast select by ID using DOM API
30.8.12.Create Node and append to XmlDocument
30.8.13.Select nodes from xml document