Read Write Xml : XmlDocument « XML « C# / CSharp Tutorial






using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;

    class Program{
        static void Main(string[] args)
        {
            XmlDocument itemDoc = new XmlDocument();
            itemDoc.Load("items.xml");
            Console.WriteLine("DocumentElement has {0} children.",itemDoc.DocumentElement.ChildNodes.Count);
            foreach (XmlNode itemNode in itemDoc.DocumentElement.ChildNodes)
            {
                XmlElement itemElement = (XmlElement)itemNode;
                Console.WriteLine("\n[Item]: {0}\n{1}", itemElement.Attributes["name"].Value,itemElement.Attributes["description"].Value);
                if (itemNode.ChildNodes.Count == 0)
                    Console.WriteLine("(No additional Information)\n");
                else
                {
                    foreach (XmlNode childNode in itemNode.ChildNodes)
                    {
                        if (childNode.Name.ToUpper() == "ATTRIBUTE")
                        {
                            Console.WriteLine("{0} : {1}",
                                childNode.Attributes["name"].Value,
                                childNode.Attributes["value"].Value);
                        }
                        else if (childNode.Name.ToUpper() == "SPECIALS")
                        {
                            foreach (XmlNode specialNode in childNode.ChildNodes)
                            {
                                Console.WriteLine("*{0}:{1}",
                                    specialNode.Attributes["name"].Value,
                                    specialNode.Attributes["description"].Value);
                            }
                        }
                    }
                }
            }
        }
    }








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