1. Books.xml内容
books.xml
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>
An in-depth look at creating applications
with XML.
</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>
A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
</description>
</book>
</catalog>
2. 使用XmlTextReader读取xml文件
XmlTextReader是一个阅读器指针,它总是向前移动,读出一块数据到内存缓冲器。比起XmlDocument类加载整个文档到内存有更好的性能。
void ReadByXmlTextReader()
{
XmlTextReader reader = new XmlTextReader("Books.xml");
reader.WhitespaceHandling = WhitespaceHandling.None;
while (reader.Read()) {
if (reader.Name == "book") {
Console.WriteLine(reader.GetAttribute("id") + ": ");
reader.Read();
string author = reader.ReadElementContentAsString();
string title = reader.ReadElementContentAsString();
string genre = reader.ReadElementContentAsString();
string price = reader.ReadElementContentAsString();
string publishDate = reader.ReadElementContentAsString();
string description = reader.ReadElementContentAsString();
Console.WriteLine("{0} book {1} written by {2}, published on {3}", genre, title, author, publishDate);
Console.WriteLine(description);
}
}
reader.Close();
}
3.使用XmlDocument和XmlNodeReader读取文件
XmlNodeReader类似于XmlTextReader,但是接受一个XmlNode实例作为读出目标。
void ReadByXmlDocument()
{
var doc = new XmlDocument();
doc.Load("Books.xml");
var nodes = doc.GetElementsByTagName("book");
foreach (XmlNode node in nodes) {
Console.WriteLine(node.Attributes["id"].Value + ":");
var reader = new XmlNodeReader(node);
reader.Read();
reader.Read();
string author = reader.ReadElementContentAsString();
string title = reader.ReadElementContentAsString();
string genre = reader.ReadElementContentAsString();
string price = reader.ReadElementContentAsString();
string publishDate = reader.ReadElementContentAsString();
string description = reader.ReadElementContentAsString();
Console.WriteLine("{0} book {1} written by {2}, published on {3}", genre, title, author, publishDate);
Console.WriteLine(description);
}
}
4.修改Xml文件
XmlLDocument没有Close或者Dispose方法,因为它是XML文档的内存表现。一旦读出,文件不再被需要。
void ChangeXmlDocument()
{
var xmlDocument = new XmlDocument();
xmlDocument.Load("Books.xml");
XmlNode nodeToModify = xmlDocument.DocumentElement.SelectSingleNode("book/genre");
nodeToModify.InnerText = "XML Tech";
XmlElement newElement = xmlDocument.CreateElement("book");
XmlAttribute newAttribute = xmlDocument.CreateAttribute("id");
newAttribute.Value = "bk103";
newElement.Attributes.Append(newAttribute);
XmlElement authorElement = xmlDocument.CreateElement("author");
authorElement.InnerText = "Mark Russinovich,David Solomon,Alex Ionecsu";
newElement.AppendChild(authorElement);
XmlElement titleElement = xmlDocument.CreateElement("title");
titleElement.InnerText = "Windows Internals, 5th edition";
newElement.AppendChild(titleElement);
XmlElement genreElement = xmlDocument.CreateElement("genre");
genreElement.InnerText = "Windows Server 2008";
newElement.AppendChild(genreElement);
XmlElement priceElement = xmlDocument.CreateElement("price");
priceElement.InnerText = "69.99";
newElement.AppendChild(priceElement);
XmlElement publishDateElement = xmlDocument.CreateElement("publish_date");
publishDateElement.InnerText = "2009-6-17";
newElement.AppendChild(publishDateElement);
XmlElement descriptionElement = xmlDocument.CreateElement("description");
descriptionElement.InnerText = "Windows Internals...";
newElement.AppendChild(descriptionElement);
xmlDocument.DocumentElement.AppendChild(newElement);
xmlDocument.Save("Books.xml");
}

浙公网安备 33010602011771号