Transform XML

G

Gina_Marano

Hey All,

I have a string that contains XML content and I want to transform it
in new XML content string.

What is the best way of doing this?

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(aXSLFileName);

//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigXMLContent)));

//create my stream for the result
Stream resultStream;
//do the transformation
xslt.Transform(readerOrigContent, null, resultStream);

//copy my stream into the StreamReader so I can get the string
StreamReader readerResult = new StreamReader(resultStream);
//get the resuling xml file as a string
return readerResult.ReadToEnd();
}

TIA!

~GINA_M~
 
G

Gina_Marano

I think i may have found it in another post:

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigXMLContent)));

XslCompiledTransform myXslTransform = new XslCompiledTransform();

// Prepare an output stream based on a System.Text.StringBuilder():
StringBuilder sbResult = new StringBuilder();
StringWriter sResult = new StringWriter(sbResult);
XmlTextWriter sXML = new XmlTextWriter(sResult);

// Load the XSL, your code:
myXslTransform.Load(aXSLFileName);
// Perform transform, your code modified to use new stream:
myXslTransform.Transform(readerOrigContent, null, sXML);
// Make sure the stream's written everything to the underlying
buffer:
sXML.Flush();
// Retrieve string:
return sbResult.ToString();
}

I will let you know the outcome.

~GINA_M~
 
K

ko

I think i may have found it in another post:

private string DoXSLTransform(string aXSLFileName, string
sOrigXMLContent)
{
//copy my string into the XmlTextReader
XmlTextReader readerOrigContent =
new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigXMLContent)));

XslCompiledTransform myXslTransform = new XslCompiledTransform();

// Prepare an output stream based on a System.Text.StringBuilder():
StringBuilder sbResult = new StringBuilder();
StringWriter sResult = new StringWriter(sbResult);
XmlTextWriter sXML = new XmlTextWriter(sResult);

// Load the XSL, your code:
myXslTransform.Load(aXSLFileName);
// Perform transform, your code modified to use new stream:
myXslTransform.Transform(readerOrigContent, null, sXML);
// Make sure the stream's written everything to the underlying
buffer:
sXML.Flush();
// Retrieve string:
return sbResult.ToString();

}

I will let you know the outcome.

~GINA_M~

Hi Gina,

If that didn't work try this:

private static string xsl_transform(string xsl_file, string
xml_content) {
// XmlReader.Create(TextReader) - StringReader 'is a' TextReader
using (XmlReader xr = XmlReader.Create(new
StringReader(xml_content))) {
XslCompiledTransform xc = new XslCompiledTransform();
xc.Load(xsl_file);
StringBuilder sb = new StringBuilder();
// XmlTextWriter(TextWriter) - StringWriter 'is a' TextWriter
using (XmlTextWriter xt = new XmlTextWriter(new
StringWriter(sb))) {
xc.Transform(xr, xt);
}
return sb.ToString();
}
}

HTH - keith
 
J

Jon Skeet [C# MVP]

new XmlTextReader(new
MemoryStream(ASCIIEncoding.Default.GetBytes(sOrigXMLContent)));

As I've already said, this is a horrible way of loading a string into
an XmlTextReader. Use StringReader.
 
J

Jon Skeet [C# MVP]

Gina_Marano said:
Can you type up a little example?

string x = "<?xml version=\"1.0\"><foo/>";

using (StringReader reader = new StringReader(x))
{
// do what you want with "reader", e.g. passing it to
// the constructor of XmlTextReader
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top