0

There are lots of examples on the web of transforming an XML file to a different format using an XSLT file, like the following:

XslTransform myXslTransform = new XslTransform();
XsltSettings myXsltSettings = new XsltSettings();
myXsltSettings.EnableDocumentFunction = true;
myXslTransform.Load("transform.xsl");
myXslTransform.Transform("input.xml", "output.xml");

However this is only a partial answer, I would like to be able to get the XML input data from a web form and use that as the input xml data instead of an '.xml' file, but have not found any concrete examples. Using Visual Studio I see Load methods that accept XmlReader objects as parameters but I do not know how to create one of those using the data from a form and TextBox control. It would be very helpful if someone could provide an example of transforming XML using form data instead of an input file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Just use `doc.LoadXml(theFormsItemValue)`, where `doc` is a new XmlDocument. Then use the overload of `Transform` that takes an XMLDocument (or `XpathNavigator`) as an argument. – Dimitre Novatchev Dec 27 '12 at 18:29

3 Answers3

0

Create a class and populate an instance of this class during postback from your form data and serialize it ( convert it to xml)

Here is console example for you

using System;
using System.Collections.Generic;

using System.Text;

using System.Xml.Serialization;

namespace Stackoverflow
{

    public class Program
    {

        static void Main(string[] args)
        {

          var p = new Person
            {
                FirstName = "Daniel",  /// in your case you get it from the form
                LastName = "Endeg"

            };

            var x = new XmlSerializer(p.GetType());
            x.Serialize(Console.Out, p);
            Console.WriteLine();
            Console.ReadLine();
        }
    }

    public class Person
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }
    }
}
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • This is a good solution, thanks for taking the time to post this answer, I think my solution I came up with is a little less elegant, although it works. – Michael G. Workman Dec 29 '12 at 18:11
0

Please note that XslTransform is obsolete since .NET 2.0, you should use XslCompiledTransform instead. And if you want to use XslSettings then make sure you pass them in to the XslCompiledTransform's Load method (e.g. http://msdn.microsoft.com/en-us/library/ms163425.aspx), simply creating it does not make sense.

As for parsing XML you have in a string variable or property (like the Text property of a TextBox) you have lots of options, you can use an XmlReader over a StringReader e.g.

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("sheet.xsl");

using (StringReader sr = new StringReader(TextBox1.Text))
{
  using (XmlReader xr = XmlReader.Create(sr))
  {
    proc.Transform(xr, null, Response.Output);
  }
}

Or you can create an XPathDocument or XmlDocument or XDocument from the string and use an overload of the Transform method that takes an IXPathNavigable as the first argument.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
-1

Ok, with some help from Visual Studio auto-complete which lists the parameters for constructors and methods, I was able to complete a working answer to the problem above, using strings for input and output in an Xslt transform operation. Yay. The example answer below assumes you have three strings containing the Xslt text data and the input Xml text data and output Xml data:

string XsltText;
string InputXML;
string OutputXml;

// define the xslt from a string
TextReader myXsltText = new StringReader(XsltText);
XmlReader myXslt = new XmlTextReader(myXsltText);

// define the input xml from a string
TextReader myXmlText = new StringReader(InputXML);
XmlReader myInputXml = new XmlTextReader(myXmlText);

// define the output XmlWriter for the results of the transform
TextWriter myOutputXmlTextWriter = new StringWriter();
XmlWriter myOutputXml = new XmlTextWriter(myOutputXmlTextWriter);

XslCompiledTransform myXslTransform = new XslCompiledTransform();
XsltSettings myXsltSettings = new XsltSettings();
myXsltSettings.EnableDocumentFunction = true;
myXslTransform.Load(myXslt);
myXslTransform.Transform(myInputXml, myOutputXml);

// the result from the transform comes from the TextWriter object
OutputXml = myOutputXmlTextWriter.ToString();

// clean up writers
myOutputXml.Flush();
myOutputXmlTextWriter.Close();
myOutputXml.Close();

To get this code working with a web form, all you have to do is get the strings from the value (Text) of the form elements (controls), for the input XMl and Xslt you could use TextBox controls, and to display the results you could use a label, all very useful, if anyone has a better answer please feel free to let me know.

  • -1: 1) Don't use `new XmlTextReader`. use `XmlReader.Create`. Also, you need to put your readers and writers and such into `using` blocks. – John Saunders Dec 27 '12 at 21:11