C#操作XMLの完全な例——XmlDocument編


これはc#コンソールプログラムの下でXmlDocumentでXML操作を行う例で、クエリー、追加、修正、削除、保存の基本操作が含まれています.1つのXMLの操作フロー全体をより完全に説明した.入門にふさわしいNetXML操作の友达は参考と勉強します.
XMLファイルがあると仮定:books.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <name>    </name>
  <price>10</price>
  <memo>         。</memo>
 </book>
 <book id="B02">
  <name>    </name>
  <price>10</price>
  <memo>      。</memo>
 </book>
 <book id="B03">
  <name>  </name>
  <price>6</price>
  <memo>      。</memo>
 </book>
 <book id="B04">
  <name>  </name>
  <price>5</price>
  <memo>      。</memo>
 </book>
</books>  

  
次はProgramです.cs
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace TestXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlElement theBook = null, theElem = null, root = null;
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load("Books.xml");
                root = xmldoc.DocumentElement;

                //---          ----
                theBook = xmldoc.CreateElement("book");
                theElem = xmldoc.CreateElement("name");
                theElem.InnerText = "  ";
                theBook.AppendChild(theElem);

                theElem = xmldoc.CreateElement("price");
                theElem.InnerText = "20";
                theBook.AppendChild(theElem);

                theElem = xmldoc.CreateElement("memo");
                theElem.InnerText = "     。";
                theBook.AppendChild(theElem);
                root.AppendChild(theBook);
                Console.Out.WriteLine("---          ----");
                Console.Out.WriteLine(root.OuterXml);
                //---          ----

                //---     《    》     。 ----
                //---     《    》----
                theBook = (XmlElement)root.SelectSingleNode("/books/book[name='    ']");
                Console.Out.WriteLine("---    《    》 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                //---             -----
                theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName    NodeList,     item(0)。  ,GetElementsByTagName("price")   SelectNodes(".//price")。
                Console.Out.WriteLine("---             ----");
                Console.Out.WriteLine(theBook.OuterXml);
                //---           id,  B01 ----
                theBook.SetAttribute("id", "B01");
                Console.Out.WriteLine("---           id,  B01 ----");
                Console.Out.WriteLine(theBook.OuterXml);
                //---   《    》    。 ----

                //---          10      ----
                theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");
                Console.Out.WriteLine("---    id    《    》    ----");
                Console.Out.WriteLine(theBook.OuterXml);
                theBook.ParentNode.RemoveChild(theBook);
                Console.Out.WriteLine("---      XML ----");
                Console.Out.WriteLine(xmldoc.OuterXml);

                //---          10      ----
                XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
                Console.Out.WriteLine("---          10      ---");
                Console.Out.WriteLine("---          " + someBooks.Count + " 。  ---");

                for (int i = 0; i < someBooks.Count; i++)
                {
                    someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
                }
                Console.Out.WriteLine("---      XML ----");
                Console.Out.WriteLine(xmldoc.OuterXml);

                xmldoc.Save("books.xml");//   books.xml

                Console.In.Read();
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
        }
    }
}