EDN Admin
Well-known member
This C# application is building an Excel spreadsheet (Xml Spreadsheet 3.0) using XmlDocument and outputting it using XmlWriter.
Some of the data fields have embedded newline characters.
When text is assigned to an XmlElement like this:
<div style="background-color:white; color:black
<pre> elem.InnerText = <span style="color:#a31515 "HellonThere";
[/code]
The resulting XML looks like this:
<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 elem<span style="color:blue >Hello
There<span style="color:blue </<span style="color:#a31515 elem<span style="color:blue >
[/code]
When Excel reads this it treats the new-line as white space and imports the text as:
<pre> Hello There[/code]
For Excel to preserve the new-line character it requires the encoding to be:
<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 elem<span style="color:blue >Hello There<span style="color:blue </<span style="color:#a31515 elem<span style="color:blue >
[/code]
I have tried using elem.InnerXml with the string already encoded as above, but it converts the encoded value back to a new-line character.
Here is a sample program to demonstrate the problem:
<div style="background-color:white; color:black
<pre> XmlDocument d = <span style="color:blue new XmlDocument();
d.AppendChild( d.CreateXmlDeclaration( <span style="color:#a31515 "1.0", <span style="color:blue null, <span style="color:blue null ) );
XmlElement e = d.CreateElement( <span style="color:#a31515 "root" );
d.AppendChild( e );
e.InnerText = <span style="color:#a31515 "HellonThere";
<span style="color:green // e.InnerXml = "Hello There"; //produces same output as InnerText
XmlWriterSettings <span style="color:blue set = <span style="color:blue new XmlWriterSettings();
<span style="color:blue set.Indent = <span style="color:blue true;
<span style="color:blue set.NewLineHandling = NewLineHandling.Entitize;
<span style="color:blue using( XmlWriter w = XmlWriter.Create( Console.Out, <span style="color:blue set ) )
{
d.WriteTo( w );
Console.WriteLine();
}
[/code]
Is there some way to get XmlElement / XmlDocument / XmlWriter to output new-line characters in the encoded form?
Thanks,
Chris.
View the full article
Some of the data fields have embedded newline characters.
When text is assigned to an XmlElement like this:
<div style="background-color:white; color:black
<pre> elem.InnerText = <span style="color:#a31515 "HellonThere";
[/code]
The resulting XML looks like this:
<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 elem<span style="color:blue >Hello
There<span style="color:blue </<span style="color:#a31515 elem<span style="color:blue >
[/code]
When Excel reads this it treats the new-line as white space and imports the text as:
<pre> Hello There[/code]
For Excel to preserve the new-line character it requires the encoding to be:
<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 elem<span style="color:blue >Hello There<span style="color:blue </<span style="color:#a31515 elem<span style="color:blue >
[/code]
I have tried using elem.InnerXml with the string already encoded as above, but it converts the encoded value back to a new-line character.
Here is a sample program to demonstrate the problem:
<div style="background-color:white; color:black
<pre> XmlDocument d = <span style="color:blue new XmlDocument();
d.AppendChild( d.CreateXmlDeclaration( <span style="color:#a31515 "1.0", <span style="color:blue null, <span style="color:blue null ) );
XmlElement e = d.CreateElement( <span style="color:#a31515 "root" );
d.AppendChild( e );
e.InnerText = <span style="color:#a31515 "HellonThere";
<span style="color:green // e.InnerXml = "Hello There"; //produces same output as InnerText
XmlWriterSettings <span style="color:blue set = <span style="color:blue new XmlWriterSettings();
<span style="color:blue set.Indent = <span style="color:blue true;
<span style="color:blue set.NewLineHandling = NewLineHandling.Entitize;
<span style="color:blue using( XmlWriter w = XmlWriter.Create( Console.Out, <span style="color:blue set ) )
{
d.WriteTo( w );
Console.WriteLine();
}
[/code]
Is there some way to get XmlElement / XmlDocument / XmlWriter to output new-line characters in the encoded form?
Thanks,
Chris.
View the full article