Xsl 转换xml 可以转换自己想要的模板
books.xslt
View Code
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:template match="/"> <HTML> <head> <title>Price List</title> </head> <body> <table> <xsl:apply-templates select="bookstore"></xsl:apply-templates> </table> </body> </HTML> </xsl:template> <xsl:template match="bookstore"> <xsl:apply-templates select="book"></xsl:apply-templates> </xsl:template> <xsl:template match="book"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="price"/> </td> <td> <xsl:value-of select="disc"/> </td> </tr> </xsl:template> </xsl:stylesheet>
booksarg.xslt
View Code
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:bookUtil="urn:XmlDemo" > <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:element name="books"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="bookstore"> <xsl:apply-templates select="book"/> </xsl:template> <xsl:template match="book"> <xsl:element name="discbook"> <xsl:element name="booktitle"> <xsl:value-of select="title"/> </xsl:element> <xsl:element name="showtext"> <xsl:value-of select="bookUtil:ShowText()"/> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet>
booksarg2.xslt
View Code
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:user="http://www.wrox.com" > <msxsl:script language="C#" implements-prefix="user"> string ShowText() { return "This came from the ShowText method!"; } </msxsl:script> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:element name="books"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="bookstore"> <xsl:apply-templates select="book"/> </xsl:template> <xsl:template match="book"> <xsl:element name="discbook"> <xsl:element name="booktitle"> <xsl:value-of select="title"/> </xsl:element> <xsl:element name="showtext"> <xsl:value-of select="user:ShowText()"/> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet>
View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; namespace XmlDemo { public class xsl { public void AddXsl() { XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xslt"); trans.Transform(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml", @"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.html"); } public void AddXslT() { XPathDocument doc = new XPathDocument(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\books.xml"); XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\booksarg.xslt"); XmlWriter xw = new XmlTextWriter(@"D:\users\c19102989\documents\visual studio 2010\projects\XmlDemo\XmlDemo\Xml\argSample.xml",null); XsltArgumentList argBook = new XsltArgumentList(); BookUtils bu = new BookUtils(); argBook.AddExtensionObject("urn:XmlDemo", bu); XPathNavigator nav = doc.CreateNavigator(); trans.Transform(nav,argBook,xw); xw.Close(); } } public class BookUtils { public int i; public BookUtils() { i = 0; } public string ShowText() { i++; return "This came from the ShowText method"+i.ToString(); } } }
View Code
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) { xsl x = new xsl(); x.AddXslT(); 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"); } } }
xsl可以序列化成自己想要的模板 可以转换html

浙公网安备 33010602011771号