C#中用XmlDocument对象获取XML文件中的节点值
XmlDocument是表示DOM的类。1.加载XML文档:使用load()方法加载XML文档;
2.读取节点:使用GetElementById()、getElementsByTagName_r()方法根据ID或标签名读取节点;
3.查找节点:使用SelectSingleNode(string
4.插入节点:使用createElement_x()方法创建节点,AppendChild()方法添加新节点;
5.创建文档:通过XmlDeclaration对象新建声明节点,其他同插入节点。
xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
</bookstore>
实例:处理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.
//创建文档
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.
}
books.xml:
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
bookstore
>
03.
<
book
genre
=
"autobiography"
publicationdate
=
"1991"
ISBN
=
"1-861003-11-0"
>
04.
<
title
>The Autobiography of Benjamin Franklin</
title
>
05.
<
author
>
06.
<
first-name
>Benjamin</
first-name
>
07.
<
last-name
>Franklin</
last-name
>
08.
</
author
>
09.
<
price
>8.99</
price
>
10.
</
book
>
11.
<
book
genre
=
"novel"
publicationdate
=
"1967"
ISBN
=
"0-201-63361-2"
>
12.
<
title
>The Confidence Man</
title
>
13.
<
author
>
14.
<
first-name
>Herman</
first-name
>
15.
<
last-name
>Melville</
last-name
>
16.
</
author
>
17.
<
price
>11.99</
price
>
18.
</
book
>
19.
<
book
genre
=
"philosophy"
publicationdate
=
"1991"
ISBN
=
"1-861001-57-6"
>
20.
<
title
>The Gorgias</
title
>
21.
<
author
>
22.
<
name
>Plato</
name
>
23.
</
author
>
24.
<
price
>9.99</
price
>
25.
</
book
>
26.
</
bookstore
>
booksEdit.xml:修改后的XML文档
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
bookstore
>
03.
<
book
genre
=
"autobiography"
publicationdate
=
"1991"
ISBN
=
"1-861003-11-0"
>
04.
<
title
>The Autobiography of Benjamin Franklin</
title
>
05.
<
author
>
06.
<
first-name
>Benjamin</
first-name
>
07.
<
last-name
>Franklin</
last-name
>
08.
</
author
>
09.
<
price
>8.99</
price
>
10.
</
book
>
11.
<
book
genre
=
"novel"
publicationdate
=
"1967"
ISBN
=
"0-201-63361-2"
>
12.
<
title
>The Confidence Man</
title
>
13.
<
author
>
14.
<
first-name
>Herman</
first-name
>
15.
<
last-name
>Melville</
last-name
>
16.
</
author
>
17.
<
price
>11.99</
price
>
18.
</
book
>
19.
<
book
genre
=
"philosophy"
publicationdate
=
"1991"
ISBN
=
"1-861001-57-6"
>
20.
<
title
>The Gorgias</
title
>
21.
<
author
>
22.
<
name
>Plato</
name
>
23.
</
author
>
24.
<
price
>9.99</
price
>
25.
</
book
>
26.
<
book
genre
=
"MyStery"
publicationdate
=
"2001"
ISBN
=
"123456789"
>
27.
<
title
>The Case of the Missing Cookie</
title
>
28.
<
author
>
29.
<
name
>C.Monster</
name
>
30.
</
author
>
31.
<
price
>9.99</
price
>
32.
</
book
>
33.
</
bookstore
>
booksCreate.xml:新建的XML文档
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<
bookstore
>
03.
<
book
genre
=
"MyStery"
publicationdate
=
"2001"
ISBN
=
"123456789"
>
04.
<
title
>The Case of the Missing Cookie</
title
>
05.
<
author
>
06.
<
name
>C.Monster</
name
>
07.
</
author
>
08.
<
price
>9.99</
price
>
09.
</
book
>
10.
</
bookstore
>