In the .NET Framework 1.1, often we needed to make transformations of XML contained in a String data type variables was not possible. The things changed in the .NET Framework 2,0, thanks to the System.Xml.Xsl.XslCompiledTransform class of the following way:
private string Transform(String xslPath, String strXML)
{
System.Xml.Xsl.XslCompiledTransform xsl = new System.Xml.Xsl.XslCompiledTransform();
xsl.Load(xslPath);
System.IO.MemoryStream msIn = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strXML));
System.IO.MemoryStream msOut = new System.IO.MemoryStream();
msIn.Position = 0;
System.Xml.XmlTextReader trInput = new System.Xml.XmlTextReader(msIn);
System.Xml.XmlWriter twOutput = System.Xml.XmlWriter.Create(msOut, xsl.OutputSettings);
xsl.Transform(trInput, twOutput);
trInput.Close();
msIn.Close();
msOut.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(msOut);
String xmlStr = String.Format("{0}", sr.ReadToEnd());
msOut.Close();
sr.Close();
return xmlStr;
}
The xslPath parameter is our XSL physical path. The strXML parameter is our XML contained in a String data type variable. In this practical example our “Transform” function will return a String data type with the result of this transformation.
String xmlTranformed = Transform(“xslEjemplo.xsl”,” ……”);
Tuesday, February 06, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment