Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I battled with this problem for a couple of days and finally came up with this solution. I used <code>XMLDocument.Save(Stream)</code> method, then got the formatted XML string from the stream. Then I replaced the <code>&amp;amp;#10;</code> occurrences with <code>&amp;#10;</code> and used the TextWriter to write the string to a file.</p> <pre><code>string xml = "&lt;?xml version=\"1.0\"?&gt;&lt;?mso-application progid='Excel.Sheet'?&gt;&lt;Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\"&gt;"; xml += "&lt;Styles&gt;&lt;Style ss:ID=\"s1\"&gt;&lt;Alignment ss:Vertical=\"Center\" ss:WrapText=\"1\"/&gt;&lt;/Style&gt;&lt;/Styles&gt;"; xml += "&lt;Worksheet ss:Name=\"Default\"&gt;&lt;Table&gt;&lt;Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"75\" /&gt;&lt;Row&gt;&lt;Cell ss:StyleID=\"s1\"&gt;&lt;Data ss:Type=\"String\"&gt;Hello&amp;amp;&amp;#35;10;&amp;amp;&amp;#35;10;World&lt;/Data&gt;&lt;/Cell&gt;&lt;/Row&gt;&lt;/Table&gt;&lt;/Worksheet&gt;&lt;/Workbook&gt;"; System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml(xml); //load the xml string System.IO.MemoryStream stream = new System.IO.MemoryStream(); doc.Save(stream); //save the xml as a formatted string stream.Position = 0; //reset the stream position since it will be at the end from the Save method System.IO.StreamReader reader = new System.IO.StreamReader(stream); string formattedXML = reader.ReadToEnd(); //fetch the formatted XML into a string formattedXML = formattedXML.Replace("&amp;amp;#10;", "&amp;#10;"); //Replace the unhelpful &amp;amp;#10;'s with the wanted endline entity System.IO.TextWriter writer = new System.IO.StreamWriter("C:\\Temp\test1.xls"); writer.Write(formattedXML); //write the XML to a file writer.Close(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload