Csharp/C Sharp/XML/XML Transform

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Illustrates the XslTransform class

<source lang="csharp"> /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110

  • /

/*

 Example20_3.cs illustrates the XslTransform class
  • /

using System; using System.Xml; using System.Xml.Xsl; using System.IO; public class Example20_3 {

   public static void Main() 
   {
       // use an XmlTextReader to open an XML document
       XmlTextReader xtr = new XmlTextReader("Cust3.xml");
       xtr.WhitespaceHandling = WhitespaceHandling.None;
       // load the file into an XmlDocuent
       XmlDocument xd = new XmlDocument();
       xd.Load(xtr);
       
       // load an XSLT file
       XslTransform xslt = new XslTransform();
       xslt.Load("Cust.xsl");
       // perform the transformation in memory
       MemoryStream stm = new MemoryStream();
       xslt.Transform(xd, null, stm);
       // and dump the results
       stm.Position = 1;
       StreamReader sr = new StreamReader(stm);
       Console.Write(sr.ReadToEnd());
       // close the reader
       xtr.Close();
   }

} //File:Cust3.xml /* <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="Cust.xsl"?> <NewDataSet>

   <Customers>
       <CustomerID>ALFKI</CustomerID>
       <CompanyName>Alfreds Futterkiste</CompanyName>
       <ContactName>Maria Anders</ContactName>
       <ContactTitle>Sales Representative</ContactTitle>
       <Address>Obere Str. 57</Address>
       <City>Berlin</City>
       <PostalCode>12209</PostalCode>
       <Country>Germany</Country>
       <Phone>030-0074321</Phone>
       <Fax>030-0076545</Fax>
   </Customers>
   <Customers>
       <CustomerID>BONAP</CustomerID>
       <CompanyName>A Company</CompanyName>
       <ContactName>Laurence Lebihan</ContactName>
       <ContactTitle>Owner</ContactTitle>
       <Address>12, rue des Bouchers</Address>
       <City>Marseille</City>
       <PostalCode>13008</PostalCode>
       <Country>France</Country>
       <Phone>91.24.45.40</Phone>
       <Fax>91.24.45.41</Fax>
   </Customers>

</NewDataSet>

  • /

//File:Cust.xsl /* <?xml version="1.0" encoding="UTF-8"?> <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <body>

   <xsl:for-each select="/NewDataSet/Customers">

Customer

        
<xsl:value-of select="CustomerID"/></br>
<xsl:value-of select="CompanyName"/></br>

<xsl:value-of select="ContactName"/></br>

   </xsl:for-each>

</body> </html>

  • /
      </source>


Perform an XSL Transform

<source lang="csharp"> using System; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Xml.Xsl;

public class TransformXml {

   private static void Main() {
       XslTransform transform = new XslTransform();
           
       // Load the XSL stylesheet.
       transform.Load("orders.xslt");
           
       // Transform orders.xml into orders.html using orders.xslt.
       transform.Transform("orders.xml", "orders.html", null);
   }

}


      </source>


Read command line input and do the xml xsl translation

<source lang="csharp"> using System; using System.Xml.Xsl; public class Transform {

 public static void Main(string [] args) {
   string source = args[0];
   string stylesheet = args[1];
   string destination = args[2];
   XslTransform transform = new XslTransform();
   transform.Load(stylesheet);
   // for .NET v 1.0
   //transform.Transform(source, destination);
   // for .NET v 1.1
   transform.Transform(source, destination, null);
 }

}


      </source>


Use XslCompiledTransform

<source lang="csharp"> using System; using System.Xml.Xsl; class MainClass {

   public static void Main() {
       XslCompiledTransform transform = new XslCompiledTransform();
       transform.Load("orders.xslt");
       transform.Transform("orders.xml", "orders.html");
   }

}

      </source>


XML transformation: transform XML file to HTML file

<source lang="csharp"> using System; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO;

 public class XSLDemo
 {
   [STAThread]
   static void Main(string[] args)
   {
     XslTransform xslt = new XslTransform();
     xslt.Load("XSLTemplate.xsl");
     XPathDocument xDoc = new XPathDocument("Books.xml");
     XmlTextWriter writer = new XmlTextWriter("Books.html", null);
     xslt.Transform(xDoc, null, writer, new XmlUrlResolver());
     writer.Close();
     StreamReader stream = new StreamReader("Books.html");
     Console.Write(stream.ReadToEnd());
   }
 }

/* <books>

 <book category="A">
   <title>title</title>
   <author>Tom</author>
   <price>19.95</price>
 </book>
 <book category="B">
   <title>title 2</title>
   <author>Jack</author>
   <price>9.95</price>
 </book>

</books>

  • /

/* <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match = "/" > <html> <head><title>A list of books</title></head> <style> .headerClass { background-color=#ffeedd; } </style> <body> List of books

<xsl:for-each select="//books/book"> </xsl:for-each>
Title Author Price
<xsl:value-of select="title"/> <xsl:value-of select="author"/> <xsl:value-of select="price"/>

</body> </html> </xsl:template> </xsl:stylesheet>

  • /
      </source>