How to save CRLF (/r/n) into '
&#10' - decimal format instead of '
&#xA' -hexadecimal format in XML file from C# application?

Nanjunda Swamy, Naveen 1 Reputation point
2022-12-06T04:38:47.673+00:00

The /r/n or the new line is converted in (Hexadecimal) instead of (Decimal) in the Windows production environment in an XML file.
The same code converts /r/n as (Decimal) as required/expected in my local environment.

C# code:


*XElement xelm = *******; //Contains xml text with /r/n
XDocument xdoc = new XDocument();
xdoc.add(xelm);
using(var strWriter = new StreamWriter(path, false, new UTF8Encoding(false)))
{
xdoc.Save(strWriter);
}*


I tried replacing text containing /r/n into [.Replace("\r\n", " ");] but it is not successful in the production server and saving in hexadecimal format.

Could anyone please suggest a solution where the code saves the data in decimal format ( ) in all the environments?

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-12-06T09:58:08.473+00:00

    Both of decimal and hexadecimal representations are valid.

    If it is important to have decimal values for some artificial reasons, then maybe consider a custom writer:

    using( var w = new MyXmlTextWriter( path, new UTF8Encoding( false ) ) )  
    {  
        xdoc.Save( w );  
    }  
    

    where MyXmlTextWriter is something like this:

    public class MyXmlTextWriter : System.Xml.XmlTextWriter  
    {  
        public MyXmlTextWriter( string filename, Encoding? encoding )   
           : base( filename, encoding )  
        {  
        }  
      
        public override void WriteString( string? text )  
        {  
            string[] a = text.Split( "\r\n" );  
      
            for( int i = 0; i < a.Length; i++ )  
            {  
                if( i > 0 )  
                {  
                    base.WriteRaw( "&#13;&#10;" );  
                }  
                base.WriteString( a[i] );  
            }  
        }  
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.