3

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?

6
  • 2
    Can you post the code for your transform too, please? Commented Apr 22, 2013 at 19:11
  • Check the Indent property Commented Apr 22, 2013 at 19:11
  • 1
    XSLT should handle \r\n as spaces, which error are you getting when you try to apply the XSLT? Commented Apr 22, 2013 at 19:14
  • i'm not getting an error. The output is the template minus the data. Commented Apr 22, 2013 at 19:16
  • 1
    Exactly where are these \r\n and exactly why do you believe this is your problem? Commented Apr 22, 2013 at 19:19

1 Answer 1

3

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

var xml = textWriter.ToString().Replace("\r\n", "\n");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.