none
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

    <Applicant>
    <UserSegment>
    <Name>John</Name>
    <Gender>M</Gender>
    </UserSegment>
    <Photo>ESN76By/EvzeXi8FYHH0gUakAkA+D4E=</Photo>
    </Applicant>

    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;
            }
        }
    
     [System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]
        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?

    Thanks,

    Tony

    • 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.WriteEndElement();
            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,

    Joyce


    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 MSDNFSF@microsoft.com.

    Friday, December 29, 2017 1:56 AM