Csharp/CSharp Tutorial/XML/Xml transformation

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

Read xsl in an XmlTextReader

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

 static void Main(string[] args)
 {
   // Create a data set object 
   DataSet ds = new DataSet("New DataSet");
     
   XmlTextReader myXmlReader = new XmlTextReader(@"c:\books.xsl");
   // Call ReadXmlSchema  
   ds.ReadXmlSchema(myXmlReader);
 
   myXmlReader.Close();
 }

}</source>

Save XML transformation result to XmlWriter

<source lang="csharp">using System; using System.Text; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Xml.Xsl; using System.Xml; public class MainClass {

   public static void Main(){
       XslCompiledTransform transform = new XslCompiledTransform();
       transform.Load("orders.xslt");
       StringBuilder htmlContent = new StringBuilder();
       XmlWriter results = XmlWriter.Create(htmlContent);
       transform.Transform("orders.xml", results);
       Console.WriteLine(htmlContent.ToString());
   }

}</source>

Transform Extension

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public class MainClass {

   public static void Main(){
      try{
               XPathDocument doc = new XPathDocument( @"Sample.xml" );
               StringWriter sw = new StringWriter();
               XmlTextWriter tw = new XmlTextWriter( sw );
               tw.Formatting = Formatting.Indented;
               tw.Indentation = 4;
               XslTransform tr = new XslTransform();
               tr.Load( @"SampleTransform.xslt" );
               tr.Transform( doc.CreateNavigator(), null, tw );
               
               tw.Close();
               sw.Close();
               Console.WriteLine( sw.ToString());
      } catch( Exception exc ) {
               Console.WriteLine( exc.Message );
      }
   }

}</source>

    Newton</tns:name>
    Curie</tns:name>
    Steve Irwin</tns:name>
</tns:root>

Transform XML document to a html document

<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Xml.Xsl; using System.Xml; public class MainClass {

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

}</source>

Use MemoryStream to hold the XmlTransform result

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

 public static void Main() 
 {
   XmlTextReader xtr = new XmlTextReader("test.xml");
   xtr.WhitespaceHandling = WhitespaceHandling.None;
   XmlDocument xd = new XmlDocument();
   xd.Load(xtr);
       
   XslTransform xslt = new XslTransform();
   xslt.Load("test.xsl");
   MemoryStream stm = new MemoryStream();
   xslt.Transform(xd, null, stm);
   stm.Position = 1;
   StreamReader sr = new StreamReader(stm);
   Console.Write(sr.ReadToEnd());
   xtr.Close();
 }

}</source>

Using Xsl to transform data from Database

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

   private static string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
   public static void Main() 
   {
       string SQL = "SELECT TOP 5 * FROM Customers";
       SqlConnection con = new SqlConnection(connectionString);
       SqlCommand com = new SqlCommand(SQL, con);
       SqlDataAdapter adapter = new SqlDataAdapter(com);
       DataSet ds = new DataSet("CustomerDataSet");
       con.Open();
       adapter.FillSchema(ds, SchemaType.Mapped, "Customers");
       adapter.Fill(ds, "Customers");
       con.Close();
       XmlDataDocument dataDoc = new XmlDataDocument(ds);
       XslTransform xsl = new XslTransform();
       xsl.Load("transform.xsl");
            
       xsl.Transform(dataDoc, null, Console.Out);
    }

}</source>

XML transformation with XPathDocument

<source lang="csharp">using System; using System.Collections; using System.Data; using System.IO; using System.Xml; using System.Xml.XPath; using System.Xml.Xsl; public class MainClass {

  public static void Main() {
     try
     {
       XPathDocument doc = new XPathDocument( "Sample.xml" );
       StringWriter sw = new StringWriter();
       XmlTextWriter tw = new XmlTextWriter( sw );
       tw.Formatting = Formatting.Indented;
       tw.Indentation = 4;
       XslTransform tr = new XslTransform();
       tr.Load( "test.xslt" );
       tr.Transform( doc.CreateNavigator(), null, tw );
   
       tw.Close();
       sw.Close();
     }
     catch( Exception exc )
     {
         Console.WriteLine( exc.Message );
     }
   }

}</source>

XSLT Transformation

<source lang="csharp">using System; using System.Xml; using System.Xml.Xsl; using System.Collections.Generic; using System.Text;

   class Program
   {
       static void Main(string[] args)
       {
           XslCompiledTransform xct = new XslCompiledTransform();
           xct.Load("itemsTransform.xslt");
           xct.Transform("items.xml", "items.html");
           
          
       }
   }</source>