I wanted to share my little xml transformer tool that gets xml and xsl input visually and outputs the transformation in a third textbox again visually. i will attach the project so anyone can use it on the fly. i kind of combined the two code snippets from the following two websites :
http://www.knowdotnet.com/articles/indentxml.html
http://www.codeproject.com/KB/cs/xsltrafo.aspx
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Windows.Forms;
namespace XMLTransformer
{
public class Transformer
{
public Transformer()
{
}
/// <summary>
/// transforms XML via XSLTransformation
/// </summary>
/// <param name="XMLPage"></param>
/// <param name="XSLStylesheet"></param>
/// <returns>The transformed Page</returns>
public static string Trafo(string XMLPage, string XSLStylesheet, out string ErrMsg)
{
//the outputs
string result = "";
ErrMsg = "";
try
{
//read XML
TextReader tr1 = new StringReader(XMLPage);
XmlTextReader tr11 = new XmlTextReader(tr1);
XPathDocument xPathDocument = new XPathDocument(tr11);
//read XSLT
TextReader tr2 = new StringReader(XSLStylesheet);
XmlTextReader tr22 = new XmlTextReader(tr2);
XslTransform xslt = new XslTransform();
xslt.Load(tr22);
//create the output stream
StringBuilder sb = new StringBuilder();
TextWriter tw = new StringWriter(sb);
//xsl.Transform (doc, null, Console.Out);
xslt.Transform(xPathDocument, null, tw);
//get result
result = sb.ToString();
}
catch (Exception ex)
{
//Console.WriteLine (ex.Message);
ErrMsg = ex.Message;
}
return result;
}//Trafo
public static string IndentXMLString(string xml)
{
string outXml = string.Empty;
MemoryStream ms = new MemoryStream();
// Create a XMLTextWriter that will send its output to a memory stream (file)
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode);
XmlDocument doc = new XmlDocument();
try
{
// Load the unformatted XML text string into an instance
// of the XML Document Object Model (DOM)
doc.LoadXml(xml);
// Set the formatting property of the XML Text Writer to indented
// the text writer is where the indenting will be performed
xtw.Formatting = Formatting.Indented;
// write dom xml to the xmltextwriter
doc.WriteContentTo(xtw);
// Flush the contents of the text writer
// to the memory stream, which is simply a memory file
xtw.Flush();
// set to start of the memory stream (file)
ms.Seek(0, SeekOrigin.Begin);
// create a reader to read the contents of
// the memory stream (file)
StreamReader sr = new StreamReader(ms);
// return the formatted string to caller
return sr.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return string.Empty;
}
}
}
}
Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Xml.XPath;
namespace XMLTransformer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string error = string.Empty;
try
{
textBox1.Text = textBox1.Text.Replace(Environment.NewLine, string.Empty);
textBox1.Text = Transformer.IndentXMLString(textBox1.Text);
textBox2.Text = textBox2.Text.Replace(Environment.NewLine, string.Empty);
textBox2.Text = Transformer.IndentXMLString(textBox2.Text);
textBox3.Text = Transformer.Trafo(textBox1.Text, textBox2.Text, out error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(error);
}
}
}
}
By the way, i was in Manhattan two weeks ago, i would love to visit by Dani to say hi and maybe Julienne Walker if she was around, but i did not have time :)
i think i am no longer as creepy as i used to be :)