Using Vs 2010 I am editing a xsl file which will be used for transformation to output an xml file. I am trying to output a carriage return ( ) and that only. I don’t want the linefeed character (x0A) to appear.
No matter what kind of stunt I do in the xsl, the outputted file always adds a LF to my CR (CRLF) and I don’t want that. The reason for why I want only the CR is that the output is parsed by a secondary system that sends sms-messages, which requires only LF.
I’m wondering if the problem is in the xsl or the transformation process.
The stylesheet:
<xsl:stylesheet xmlns:l="http://schemas.something.no/2008/" version='1.0'>
<xsl:import href='LydiaDateFuncs.xsl'/>
<xsl:template match='/'>
<xsl:apply-imports/>
</xsl:template>
<xsl:output method="xml" encoding="utf-8" indent="no" />
<xsl:template match="l:WorkOrderOccurrenceBE">
<Message>
<Body>
Adding cr here<xsl:text>
</xsl:text><xsl:text>
</xsl:text>No dice.
</Body>
</Message>
</xsl:template>
The code for transformation:
public static XmlDocument TransformObject(string xslFilepath, object serObj)
{
XmlDocument result = null;
xslTransf.Load(xslFilepath, new XsltSettings { EnableScript = true }, newXmlUrlResolver());
result = TransformObject(xslTransf, serObj);
}
public static XmlDocument TransformObject(XslCompiledTransform xslTransform, object transformObject)
{
XmlDocument result = null;
if (xslTransform != null)
{
DataContractSerializer serializer = new DataContractSerializer(transformObject.GetType());
string serializedObjValue = null;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, transformObject);
serializedObjValue = (new UTF8Encoding()).GetString(stream.ToArray());
}
if (serializedObjValue != null)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(serializedObjValue);
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);
using (Stream stream = new MemoryStream())
{
xslTransform.Transform(xmldoc, null, stream);
stream.Position = 0;
using (StreamReader sr = new StreamReader(stream, Encoding.Unicode))
{
string xmlRes = sr.ReadToEnd();
result = new XmlDocument();
result.LoadXml(xmlRes);
}
}
}
}
return result;
}
Anybody been experiencing a similar issue?
One way to solve this problem is to use an
XmlTextWriterin a respective overload ofXslCompiledTransform.Transform()This instnce of
XmlTextWritermust have itsSettingsproperty set with an instance ofXmlWriterSettingsfor which you have specified whatever newline characters you want as the value of theXmlWriterSettings.NewLineCharsproperty.See: http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.settings.aspx
and
http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.newlinechars.aspx