C#中用XmlDocument对象获取XML文件中的节点值
XmlDocument是表示DOM的类。
1.加载XML文档:使用load()方法加载XML文档;
2.读取节点:使用GetElementById()、getElementsByTagName_r()方法根据ID或标签名读取节点;
3.查找节点:使用SelectSingleNode(string search)方法通过XPath查找节点;
4.插入节点:使用createElement_x()方法创建节点,AppendChild()方法添加新节点;
5.创建文档:通过XmlDeclaration对象新建声明节点,其他同插入节点。
xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.98</price>
</book>
<bookstore>
<title lang="eng">Learning C#</title>
<price>55.23</price>
</bookstore>
<item name="1">第一个item</item>
<item name="2">
<item name="1">这个结点(1)</item>
<item name="2">这个结点(2)</item>
<book>
<title lang="cn">Learning C</title>
<price>60.95</price>
</book>
</item>
</bookstore>
XmlDocument xdoc = new XmlDocument();
xdoc.Load(Server.MapPath("books.xml"));
XmlNodeList xnl = xdoc.SelectNodes("/bookstore/book");
string nodeTempStr = "";
foreach (XmlNode node in xnl)
{
nodeTempStr = node.InnerText;
node ......
实例:处理books.xml文档
TestXmlDocument.cs:
001.using System;
002.using System.Xml;
003.
004.namespace Magci.Test.XML.TestXmlDocment
005.{
006.class Program
007.{
008.private static XmlDocument doc;
009.
010.static void Main(string[] args)
011.{
012.doc = new XmlDocument();
013.//加载XML文档
014.doc.Load(@"..\..\books.xml");
015.DisplayTitle();
016.SearchByTitle("The Gorgias");
017.Insert();
018.CreateDoc();
019.
020.Console.ReadLine();
021.}
022.
023.//遍历节点
024.public static void DisplayTitle()
025.{
026.XmlNodeList nodes = doc.getElementsByTagName_r("title");
027.Console.WriteLine("Title:");
028.foreach (XmlNode node in nodes)
029.{
030.Console.WriteLine(node.InnerText);
031.}
032.}
033.
034.//根据title查找
035.public static void SearchByTitle(string title)
036.{
037.string search = "bookstore/book[title='" + title + "']";
038.XmlNode foundNode = doc.SelectSingleNode(search);
039.Console.WriteLine("\nSearch by title:");
040.if (foundNode != null)
041.{
042.Console.WriteLine(foundNode.InnerText);
043.}
044.else
045.{
046.Console.WriteLine("Not Found!");
047.}
048.}
049.
050.//插入节点
051.public static void Insert()
052.{
053.//创建book元素
054.XmlElement newBook = doc.createElement_x("book");
055.newBook.SetAttribute("genre", "MyStery");
056.newBook.SetAttribute("publicationdate", "2001");
057.newBook.SetAttribute("ISBN", "123456789");
058.
059.//创建title元素
060.XmlElement newTitle = doc.createElement_x("title");
061.newTitle.InnerText = "The Case of the Missing Cookie";
062.//将title元素添加到book元素中
063.newBook.AppendChild(newTitle);
064.
065.XmlElement newAuthor = doc.createElement_x("author");
066.newBook.AppendChild(newAuthor);
067.
068.XmlElement newName = doc.createElement_x("name");
069.newName.InnerText = "C.Monster";
070.newAuthor.AppendChild(newName);
071.
072.XmlElement newPrice = doc.createElement_x("price");
073.newPrice.InnerText = "9.99";
074.newBook.AppendChild(newPrice);
075.
076.//将新建的book元素加入到XML文档中
077.doc.DocumentElement.AppendChild(newBook);
078.
079.//另存为
080.XmlTextWriter tr = new XmlTextWriter(@"..\..\booksEdit.xml", null);
081.tr.Formatting = Formatting.Indented;
082.doc.WriteContentTo(tr);
083.tr.Close();
084.Console.WriteLine("\nbooksEdit.xml Saved successful.\n");
085.
086.XmlNodeList nodes = doc.getElementsByTagName_r("title");
087.Console.WriteLine("Display title after Insert:");
088.foreach (XmlNode node in nodes)
089.{
090.Console.WriteLine(node.InnerText);
091.}
092.}
093.
094.//创建文档 www.zzzyk.com
095.public static void CreateDoc()
096.{
097.XmlDocument newDoc = new XmlDocument();
098.
099.//创建声明节点
100.XmlDeclaration newDec = newDoc.CreateXmlDeclaration("1.0", "utf-8", null);
101.newDoc.AppendChild(newDec);
102.
103.XmlElement newRoot = newDoc.createElement_x("bookstore");
104.newDoc.AppendChild(newRoot);
105.
106.XmlElement newBook = newDoc.createElement_x("book");
107.newBook.SetAttribute("genre", "MyStery");
108.newBook.SetAttribute("publicationdate", "2001");
109.newBook.SetAttribute("ISBN", "123456789");
110.
111.XmlElement newTitle = newDoc.createElement_x("title");
112.newTitle.InnerText = "The Case of the Missing Cookie";
113.newBook.AppendChild(newTitle);
114.
115.XmlElement newAuthor = newDoc.createElement_x("author");
116.newBook.AppendChild(newAuthor);
117.
118.XmlElement newName = newDoc.createElement_x("name");
119.newName.InnerText = "C.Monster";
120.newAuthor.AppendChild(newName);
121.
122.XmlElement newPrice = newDoc.createElement_x("price");
123.newPrice.InnerText = "9.99";
124.newBook.AppendChild(newPrice);
125.
126.newRoot.AppendChild(newBook);
127.
128.XmlTextWriter tr = new XmlTextWriter(@"..\..\booksCreate.xml", null);
129.tr.Formatting = Formatting.Indented;
130.newDoc.WriteContentTo(tr);
131.tr.Close();
132.Console.WriteLine("\nbooksCreate.xml Saved successful.\n");
133.
134.XmlNodeList nodes = newDoc.getElementsByTagName_r("title");
135.Console.WriteLine("Display title after Create:");
136.foreach (XmlNode node in nodes)
137.{
138.Console.WriteLine(node.InnerText);
139.}
140.}
141.}
142.}
bo
补充:综合编程 , 其他综合 ,