views:

413

answers:

1

I'm trying to create a spreadsheet in XML Spreadsheet 2003 format (so Excel can read it). I'm writing out the document using the XDocument class, and I need to get a newline in the body of one of the <Cell> tags. Excel, when it reads and writes, requires the files to have the literal string &#10; embedded in the string to correctly show the newline in the spreadsheet. It also writes it out as such.

The problem is that XDocument is writing CR-LF (\r\n) when I have newlines in my data, and it automatically escapes ampersands for me when I try to do a .Replace() on the input string, so I end up with &amp;#10; in my file, which Excel just happily writes out as a string literal.

Is there any way to make XDocument write out the literal &#10; as part of the XML stream? I know I can do it by deriving from XmlTextWriter, or literally just writing out the file with a TextWriter, but I'd prefer not to if possible.

+3  A: 

I wonder if it might be better to use XmlWriter directly, and WriteRaw?

A quick check shows that XmlDocument makes a slightly better job of it, but xml and whitespace gets tricky very quickly...

Marc Gravell
Agreed, though I suggest XmlWriter.WriteWhitespace(), not WriteRaw, to output specific characters for whitespace.
Dour High Arch
Looking like I'm going to have to go that way.
stames