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代码
<?<SPAN class=hilite2>xml</SPAN> 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 read<SPAN class=hilite2>Xml</SPAN>Data() { <SPAN class=hilite2>Xml</SPAN>TextReader reader = new <SPAN class=hilite2>Xml</SPAN>TextReader("C:/books.<SPAN class=hilite2>Xml</SPAN>"); 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 write<SPAN class=hilite2>Xml</SPAN>Data() { <SPAN class=hilite2>Xml</SPAN>TextWriter writer = new <SPAN class=hilite2>Xml</SPAN>TextWriter("C:\\movie.<SPAN class=hilite2>xml</SPAN>" , null); try { writer.Formatting = Formatting.Indented; writer.Indentation = 4; //writer.<SPAN class=hilite2>Xml</SPAN>Space = 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 append<SPAN class=hilite2>Xml</SPAN>Node() { try { <SPAN class=hilite2>Xml</SPAN>Document doc = new <SPAN class=hilite2>Xml</SPAN>Document(); doc.Load("C:\\movie.<SPAN class=hilite2>xml</SPAN>" ); <SPAN class=hilite2>Xml</SPAN>Node root = doc.DocumentElement; <SPAN class=hilite2>Xml</SPAN>Element movieElement = doc.CreateElement("movie"); <SPAN class=hilite2>Xml</SPAN>Element name = doc.CreateElement("name"); name.InnerText = "Brazil"; movieElement.AppendChild(name); <SPAN class=hilite2>Xml</SPAN>Element category = doc.CreateElement("category"); category.InnerText = "Fantasy"; movieElement.AppendChild(category); <SPAN class=hilite2>Xml</SPAN>Element releaseRegionElement = doc.CreateElement("releaseRegion"); releaseRegionElement.InnerText = "USA"; movieElement.AppendChild(releaseRegionElement); <SPAN class=hilite2>Xml</SPAN>Element releaseTimeElement = doc.CreateElement("releaseTime"); releaseTimeElement.InnerText = "1985-12-18" ; movieElement.AppendChild(releaseTimeElement); <SPAN class=hilite2>Xml</SPAN>Element directorElement = doc.CreateElement("director"); directorElement.InnerText = "Terry Gilliam"; movieElement.AppendChild(directorElement); <SPAN class=hilite2>Xml</SPAN>Element priceElement = doc.CreateElement("price"); priceElement.InnerText = "29.98"; movieElement.AppendChild(priceElement); <SPAN class=hilite2>Xml</SPAN>Element 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() { <SPAN class=hilite2>Xml</SPAN>Document doc = new <SPAN class=hilite2>Xml</SPAN>Document(); doc.Load("C:\\movie.<SPAN class=hilite2>xml</SPAN>"); 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); } }