Gerson
Gerson

Reputation: 429

XML Serialization to String

I'm trying to serialize an object into a string. Here is the code:

XmlSerializer xmlSerializer = new XmlSerializer(data.GetType());
StringWriter textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, data);
var xml = textWriter.ToString();

This works but "\r\n" are part of the string. I want to perform an XSLT transform with this string. That doesn't work because of the "\r\n" characters.

Here is the transform code:

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltPath);

using (XmlReader xmlReader = System.Xml.XmlReader.Create(new StringReader(xmlString)))
{
     transform.Transform(xmlReader, xmlWriter);
     ...
}

How to I go about this?

Upvotes: 3

Views: 1113

Answers (1)

Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

Simply replace those \r\ns with \n then use XSLT

var xml = textWriter.ToString().Replace("\r\n", "\n");

Upvotes: 3

Related Questions