Prevent encoding of special characters in XML RRS feed

  • Question

  • I developed a c# application which receive data from user and then generate and save a XML file to a physical path.

    1. User input personal information in string format in the application

    e.g.: <Name>John</Name><Gender>M</Gender>

    2. The application get images from database

    3. The application assign personal information (UserSegment) and image to an object

    4. The application serialize the object to XML

    We found that special character < is converted to &lt; and > is converted to &gt;

    We expect the output XML like this


    The following is part of my code

    Object class:

    private string saSegmentField;
    private byte[] photoField;
     public string SaSegment
            get {
                return this.saSegmentField;
            set {
                this.saSegmentField = value;
        public byte[] Photo {
            get {
                return this.photoField;
            set {
                this.photoField = value;

    Main program:

    ApplicantDetails objAppDetails = new ApplicantDetails { Applicant = new ApplicantDetailsApplicant { UserSegment = strUserInput, Photo = app.Photo } }

    XmlSerializer SerializerObj = new XmlSerializer(typeof(ApplicantDetails));

    How to prevent encoding of special characters in XML?



    • Moved by 宝宝徐 Friday, December 29, 2017 1:58 AM
    Thursday, December 28, 2017 4:10 AM

All replies

  • Try custom serialisation using IXmlSerializable:


    public class ApplicantDetailsApplicant : IXmlSerializable
        public string SaSegment { get; set; }
        public byte[] Photo { get; set; }
        public XmlSchema GetSchema()
            return null;
        public void WriteXml( XmlWriter writer )
            writer.WriteStartElement( "UserSegment" );
            writer.WriteRaw( SaSegment );
            writer.WriteElementString( "Photo", Convert.ToBase64String( Photo ) );
        public void ReadXml( XmlReader reader )
            throw new NotSupportedException();


    It assumes that the user will enter good values. Maybe it is better to reconsider the approach.

    Thursday, December 28, 2017 6:03 AM
  • Hi Tony,

    I will move your thread to c# forum, if you have problems about visual studio IDE, please feel free to let me know.

    Best regards,


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Friday, December 29, 2017 1:56 AM