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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / CSharp Tutorial » XML » XmlDocument 
30.8.2.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");
      }
   }
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.