XML XSD XPathNavigator
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.XPath; namespace XmlDemo { class Program { static void Main(string[] args) { AddXPathDocument(); Console.ReadKey(); } //加载xml public static void ReadXml() { XmlReader read = XmlReader.Create(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); while (!read.EOF) { if (read.Name == "title") { Console.WriteLine(read.ReadString()); read.Read(); } else { read.Read(); } } } /// <summary> /// 用XSD文件验证xml格式是是否是自己的要的格式 /// </summary> public static void CheckingXml() { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(null, @"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xsd"); settings.ValidationType = ValidationType.Schema; settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(Sample); XmlReader read = XmlReader.Create(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml", settings); while (read.Read()) { Console.WriteLine(read.Name); } } public static void Sample(object sender, ValidationEventArgs e) { Console.WriteLine("错误"); } /// <summary> /// 根据XSD格式添加xml /// </summary> public static void CheckingWriter() { XmlWriterSettings setting = new XmlWriterSettings(); setting.Indent = true; setting.NewLineOnAttributes = true; XmlWriter writer = XmlWriter.Create(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml", setting); writer.WriteStartElement("book"); writer.WriteAttributeString("genre", "Mystery"); writer.WriteAttributeString("publicationdate", "2001"); writer.WriteAttributeString("ISBN", "123456789"); writer.WriteElementString("title", "Case of the Missing Cookie"); writer.WriteStartElement("author"); writer.WriteElementString("name", "Cookie Monster"); writer.WriteEndElement(); writer.WriteElementString("price", "9.999"); writer.WriteEndElement(); writer.WriteEndDocument(); //clean up writer.Flush(); writer.Close(); } public static void GetXmlDocument() { XmlDocument doc = new XmlDocument(); doc.Load(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XmlNodeList nodeList = doc.GetElementsByTagName("title"); foreach (XmlNode n in nodeList) { Console.WriteLine(n.OuterXml); } doc.SelectNodes("/bookstore/book/title"); } public static void AddXmlDocument() { XmlDocument _doc = new XmlDocument(); _doc.Load(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XmlElement newBook = _doc.CreateElement("book"); //set some attributes newBook.SetAttribute("genre", "Mystery"); newBook.SetAttribute("publicationdate", "2001"); newBook.SetAttribute("ISBN", "123456789"); //create a new 'title' element XmlElement newTitle = _doc.CreateElement("title"); newTitle.InnerText = "Case of the Missing Cookie"; newBook.AppendChild(newTitle); //create new author element XmlElement newAuthor = _doc.CreateElement("author"); newBook.AppendChild(newAuthor); //create new name element XmlElement newName = _doc.CreateElement("name"); newName.InnerText = "Cookie Monster"; newAuthor.AppendChild(newName); //create new price element XmlElement newPrice = _doc.CreateElement("price"); newPrice.InnerText = "9.95010"; newBook.AppendChild(newPrice); //add to the current document _doc.DocumentElement.AppendChild(newBook); //write out the doc to disk //_doc.Save(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XmlTextWriter tr = new XmlTextWriter(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml", null); tr.Formatting = Formatting.Indented; _doc.WriteContentTo(tr); tr.Close(); } /// <summary> /// XPathDocument创建只能只读 /// </summary> public static void GetXPathDocunment() { XPathDocument doc = new XPathDocument(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator(); XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']"); while (iter.MoveNext()) { XPathNodeIterator newIter = iter.Current.SelectDescendants(XPathNodeType.Element,false); Console.WriteLine(iter.Current.Name); while (newIter.MoveNext()) { Console.WriteLine(newIter.Current.Name); } } Console.WriteLine("price{0}",nav.Evaluate("sum(/bookstore/book/price)")); } public static void AddXPathDocument() { XmlDocument doc = new XmlDocument(); doc.Load(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XPathNavigator nav = doc.CreateNavigator(); if (nav.CanEdit) { XPathNodeIterator iter = nav.Select("/bookstore/book/price"); while (iter.MoveNext()) { iter.Current.InsertAfter("<disc>5</disc>"); } } doc.Save(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); } } }
_doc.CreateXmlDeclaration("1.0",null,null);//创建xml的头
XSD验证xml格式
浙公网安备 33010602011771号