关键字: C# XML XPath
C#下处理xml是通过XML.NET API类来实现的. XML.NET构架是XML DOM接口的Wrapper,在高层提供了对xml编程的实现.
XML.NET的核心类是XmlDocument, XmlReader, and XmlWriter,被包括在System.xml命名空间中.
XmlReader和XmlWriter是用于读写XML数据的抽象基类.XmlTextReader,XmlValidatingReader,XmlNodeReader是XmlReader的具体实现;而XmlTextWriter和XmlNodeWriter则是XmlWriter的具体实现.XmlDocument类通过和XmlNode, XmlElement, and XmlAttribute类一起以树形的结构形式代表了一个具体的Xml文档.
System.Xml.Xsl接口和其具体类提供了对XSLT的实现;XPathDocument类和XPathNavigator类则提供了对Xml遍历等的相关操作.
现在以下面的movie.xml文档为例(被保存在C盘下C:\movie.xml),来说明如何对Xml文档进行操作.
<?xml version="1.0" encoding="utf-8"?>
<movies>
<movie>
<name>Blade Runner</name>
<category>Sci-Fi</category>
<releaseRegion>USA</releaseRegion>
<releaseTime>1982-6-25</releaseTime>
<director>Ridley Scott</director>
<price>27.95</price>
<isrc>US-F18-06-0428-0/V.J9</isrc>
</movie>
<movie>
<name>Brazil</name>
<category>Fantasy</category>
<releaseRegion>USA</releaseRegion>
<releaseTime>1985-12-18</releaseTime>
<director>Terry Gilliam</director>
<price>29.98</price>
<isrc>US-F18-07-8428-0/V.J9</isrc>
</movie>
</movies>
读xml文档
public void readXmlData()
{
XmlTextReader reader = new XmlTextReader("C:/books.Xml");
while (reader.Read())
{
if (reader.HasValue)
{
Console.WriteLine("Name : "+ reader. Name);
Console.WriteLine("Node Depth: " + reader.Depth.ToString( ));
Console.WriteLine("Value : " + reader.Value);
}
}
}
写xml文档
public void writeXmlData()
{
XmlTextWriter writer = new XmlTextWriter("C:\\movie.xml" , null);
try {
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
//writer.XmlSpace = false;
writer.WriteStartDocument();
writer.WriteStartElement("movies");
writer.WriteStartElement("movie");
writer.WriteStartElement("name", "");
writer.WriteString("Blade Runner");
writer.WriteEndElement();
writer.WriteStartElement("category", "");
writer.WriteString("Sci-Fi");
writer.WriteEndElement();
writer.WriteStartElement("releaseRegion", "");
writer.WriteString("USA");
writer.WriteEndElement();
writer.WriteStartElement("releaseTime", "");
writer.WriteString("1982-6-25");
writer.WriteEndElement();
writer.WriterStartElement("director","");
writer.WriteString("Ridley Scott");
writer.WriterEndElement();
writer.WriterStartElement("price","");
writer.WriteString("27.95");
writer.WriterEndElement();
writer.WriteStartElement("isrc", "");
writer.WriteString("US-F18-06-0428-0/V.J9");
writer.WriteEndElement();
write.WriteEndElement();
write.WriteEndDocument();
write.Flush();
}catch (Exception e) {
Console.WriteLine("Exception: {0}", e.ToString());
}
}
在xml中追加节点内容(movie节点)
public void appendXmlNode()
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\movie.xml" );
XmlNode root = doc.DocumentElement;
XmlElement movieElement = doc.CreateElement("movie");
XmlElement name = doc.CreateElement("name");
name.InnerText = "Brazil";
movieElement.AppendChild(name);
XmlElement category = doc.CreateElement("category");
category.InnerText = "Fantasy";
movieElement.AppendChild(category);
XmlElement releaseRegionElement = doc.CreateElement("releaseRegion");
releaseRegionElement.InnerText = "USA";
movieElement.AppendChild(releaseRegionElement);
XmlElement releaseTimeElement = doc.CreateElement("releaseTime");
releaseTimeElement.InnerText = "1985-12-18" ;
movieElement.AppendChild(releaseTimeElement);
XmlElement directorElement = doc.CreateElement("director");
directorElement.InnerText = "Terry Gilliam";
movieElement.AppendChild(directorElement);
XmlElement priceElement = doc.CreateElement("price");
priceElement.InnerText = "29.98";
movieElement.AppendChild(priceElement);
XmlElement isrcElement = doc.CreateElement("isrc");
isrcElement.InnerText = "US-F18-06-0428-0/V.J9";
movieElement.AppendChild(isrcElement);
root.AppendChild(movieElement);
doc.Save(Constants .movieFile );
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
遍历xml文档
System.Xml.XPath包中的XPathNavigator,XPathNodeIterator,XPathExpression等类实现了通过XPath语法遍历,获取具体节点相关信息的操作.
public void traverse()
{
XmlDocument doc = new XmlDocument();
doc.Load("C:\\movie.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select("/movies/movie[name[contains(text(),'" + "Blade" + "')]]");
int count = iterator.Count;
Console.WriteLine("count: "+ count);
while(iterator .MoveNext() )
{
XPathNavigator movieNav = iterator.Current;
movieNav.MoveToFirstChild();
String name = movieNav.Value.ToString();
Console.WriteLine("name: "+ name);
}
}