The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Le magie di XslCompiledTransform

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Adrian Florea

Posts: 206
Nickname: adrian11
Registered: Jul, 2004

Adrian Florea is a .NET developer from Italy
Le magie di XslCompiledTransform Posted: Oct 5, 2005 6:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Adrian Florea.
Original Post: Le magie di XslCompiledTransform
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Adrian Florea
Latest Posts From Web Log di Adrian Florea

Advertisement

Appena ho visto la nuova XslCompiledTransform (in 2.0) è stato colpo di fulmine. E da questo esempietto fatto al volo, solo per rendere l'idea, capirete subito perché:

using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
 
class Test
{
      static void Main()
      {
            string methodName = "Foo";
            StringBuilder methodCode = new StringBuilder();
 
            methodCode.AppendLine  ("// ritorna la stringa Ciao ragazzi!");
            methodCode.AppendFormat("public string {0}()", methodName);
            methodCode.AppendLine  ("{");
            methodCode.AppendLine  ("   StringBuilder sb = new StringBuilder();");
            methodCode.AppendLine  ("   sb.Append(\"Ciao \");");
            methodCode.AppendLine  ("   sb.Append(\"ragazzi!\");");
            methodCode.AppendLine  ("   return sb.ToString();");
            methodCode.AppendLine  ("}");
 
            Console.WriteLine(ExecuteMethodCode(methodCode.ToString(), methodName));
      }
 
      static string ExecuteMethodCode(string methodCode, string methodName)
      {
            StringBuilder xsl = new StringBuilder();
            StringBuilder result = new StringBuilder();
 
            string prefix = "my-prefix";
            string urn = "my-urn";
 
            xsl.AppendLine("");
            xsl.AppendLine(");
            xsl.AppendLine("xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"");
            xsl.AppendLine("xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\"");
            xsl.AppendFormat("xmlns:{0}=\"urn:{1}\">", prefix, urn);
            xsl.AppendFormat("", prefix);
            xsl.AppendFormat("", methodCode);
            xsl.AppendLine("");
            xsl.AppendLine("");
            xsl.AppendFormat("", prefix, methodName);
            xsl.AppendLine("");
            xsl.AppendLine("");
 
            XmlDocument dummyXml = new XmlDocument();
            dummyXml.CreateXmlDeclaration("1.0", null, null);
            dummyXml.CreateElement(new Guid().ToString());
 
            XmlReader reader = XmlReader.Create(new StringReader(xsl.ToString()));
            XmlTextWriter writer = new XmlTextWriter(new StringWriter(result));
 
            XslCompiledTransform compiledXsl = new XslCompiledTransform();
            compiledXsl.Load(reader, XsltSettings.TrustedXslt, null);
            compiledXsl.Transform(dummyXml, writer);
 
            return result.ToString();
      }
}

Cosa stampa a console? Ciao ragazzi! naturalmente... Bellissimo!

Read: Le magie di XslCompiledTransform

Topic: NewsGator party at Web 2.0 Previous Topic   Next Topic Topic: NewsGator Enterprise Server ships!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use