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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.