Serialization of object causes blanks to be truncated

I am serializing an object and passing the string xml to the server for porcessing.

If I enter a value of "XYZ     ",  for one of the fields of the custom object , what gets converted in the serialization process is "XYZ"
How do I maintain the blanks entered in any of the fields of the object
XmlSerializer serializer = new XmlSerializer(typeof(CustomClass)); 
            MemoryStream memoryStream = new MemoryStream(); 
            serializer.Serialize(memoryStream, adjustments);        
            memoryStream.Position = 0; 
            StreamReader streamreader = new StreamReader(memoryStream);
            string strxml = UTF8ByteArrayToString(memoryStream.ToArray());
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(memoryStream);           
            xdoc.RemoveChild(xdoc.FirstChild); 
            xdoc.DocumentElement.Attributes.RemoveAll();
            
            strxml = xdoc.InnerXml;

Open in new window

LVL 1
countrymeisterAsked:
Who is Participating?
 
SriVaddadiCommented:
You can set PreserveWhitespace property of XmlDocument to true.

Check this for more info

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.preservewhitespace.aspx
0
 
Mahmoud_Al_HattabCommented:
Dear countrymeister,

 this is weird as I have tried your code and it keeps spaces. Anyways, find in the code something that I hope it to help. It is another way for xml serialization. Some other code but the same serialization.
public class Nayif
    {
        public string Value;

        public string MahHatCode()
        {
            XmlWriterSettings xmlwritersettings;
            xmlwritersettings = new XmlWriterSettings();
            xmlwritersettings.CheckCharacters = true;
            xmlwritersettings.CloseOutput = true;
            xmlwritersettings.ConformanceLevel = ConformanceLevel.Document;
            xmlwritersettings.Indent = true;
            xmlwritersettings.Encoding = Encoding.UTF8;
            xmlwritersettings.OmitXmlDeclaration = true;

            StringBuilder sb = new StringBuilder();
            XmlWriter xmlwriter = XmlWriter.Create(sb, xmlwritersettings);
            XmlSerializer xmlserializer = new XmlSerializer(this.GetType());

            xmlserializer.Serialize(xmlwriter, this);

            return sb.ToString();
        }

        public string EECode()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Nayif));
            MemoryStream memoryStream = new MemoryStream();
            serializer.Serialize(memoryStream, this);
            memoryStream.Position = 0;
            StreamReader streamreader = new StreamReader(memoryStream);
            string strxml = UTF8ByteArrayToString(memoryStream.ToArray());
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(memoryStream);
            xdoc.RemoveChild(xdoc.FirstChild);
            xdoc.DocumentElement.Attributes.RemoveAll();

            strxml = xdoc.InnerXml;

            return strxml;
        }

        private string UTF8ByteArrayToString(byte[] p)
        {
            return Encoding.UTF8.GetString(p);
        }

        public static void Main()
        {
            Nayif n = new Nayif
            {
                Value = "XYZ   "
            };

            string s = n.MahHatCode();

            string s2 = n.EECode();

            Console.WriteLine(s);
            Console.WriteLine("-------------------------------------");
            Console.WriteLine(s2);
        }
    }

Open in new window

0
 
Mahmoud_Al_HattabCommented:
both codes keeps the spaces in the generated xml.

if you still have any queries please do not hesitate.

regards,
Mahmoud Al-Hattab
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.