none
Serializing works but deserializing is missing data. RRS feed

  • Question

  • I am exposing a POST method in a wcf service for a given set of nested classes. Everything works fine, even serializing, except when I post the *same* xml I create through serializing an instance, the Umber.Amethysts property is empty.
    [XmlRoot("MyRoot", Namespace = "myNamespace")]
    public class MyRoot
    {
        private Indigo indigo;
    
        public MyRoot()
        {
            this.indigo = new Indigo();
        }
    
        [XmlElement("Indigo")]
        public Indigo Indigo
        {
            get
            {
                return this.indigo;
            }
    
            set
            {
                this.indigo = value;
            }
        }
    }
    
    public class Indigo
    {
        private Maroon maroon;
        private Umber umber;
    
        public Indigo()
        {
            this.maroon = new Maroon();
            this.umber = new Umber();
        }
    
        [XmlElement("Maroon")]
        public Maroon Maroon
        {
            get
            {
                return this.maroon;
            }
    
            set
            {
                this.maroon = value;
            }
        }
    
        [XmlElement("Umber")]
        public Umber Umber
        {
            get
            {
                return this.umber;
            }
    
            set
            {
                this.umber = value;
            }
        }
    }
    
    public class Umber
    {
        private List<Amethyst> amethysts;
    
        public Umber()
        {
            this.amethysts = new List<Amethyst>();
        }
    
        [XmlElement("Amethysts", Namespace = "")]
        public List<Amethyst> Amethysts
        {
            get
            {
                return this.amethysts;
            }
    
            set
            {
                this.amethysts = value;
            }
        }
    }
    
    public class Amethyst
    {
        private string name;
        private string value;
    
        [XmlElement("Name")]
        public string Name
        {
            get
            {
                return this.name;
            }
    
            set
            {
                this.name = value;
            }
        }
    
        [XmlElement("Value")]
        public string Value
        {
            get
            {
                return this.value;
            }
    
            set
            {
                this.value = value;
            }
        }
    }
    If I manually create the instance:
    MyRoot data = new MyRoot
    {
        Indigo = new Indigo
        {
            Maroon = new Maroon
            {
                SomeField = "text..."
            },
            Umber = new Umber
            {
                Amethysts = new List<Amethyst>
                {
                    new Amethyst { Name = "foo", Value = "bar"},
                    new Amethyst { Name = "baz", Value = "biz"}
                }
            }
        }
    };
    It serializes a pair of <Amethyst> tags but posting this very same serialized text representation back leaves the Umber element empty?

    Anyone know what I am missing?
    Thanks!


    • Edited by Ritmo2k Tuesday, March 17, 2015 10:30 PM
    Tuesday, March 17, 2015 10:28 PM

Answers

  • Hi Amy,
    It turns out the default serialization engine in WCF is of course DataContract and my usage outside was all XmlSerializer and this is where the failures had manifested. I figured this out by creating the test case for you:) I need the order independent serialization that XmlSerializer facilitates and so I changed the WCF service over to use it as well and everything works.

    In addition, the Umber class is redundant and I was able to remove that and use XmlArray and XmlArrayItem on a public property named "Umber" in the Indigo class typed as List<Amethyst>.

    Much appreciated again!
    Wednesday, March 18, 2015 4:10 PM

All replies

  • Hi Ritmo2k,

    I have tested your code in my side, then in oder to get the xml data, I have created the following OperationContract GetDataUsingDataContract() with the RequestFormat=WebMessageFormat.Xml
    and ResponseFormat=WebMessageFormat.Xml:

    public MyRoot GetDataUsingDataContract()
            {
                MyRoot data = new MyRoot
                {
                    Indigo = new Indigo
                    {
                        Maroon = new Maroon
                        {
                            SomeField = "text..."
                        },
                        Umber = new Umber
                        {
                            Amethysts = new List<Amethyst>
                {
                    new Amethyst { Name = "foo", Value = "bar"},
                    new Amethyst { Name = "baz", Value = "biz"}
                }
                        }
                    }
                };
                return data;
            }
        }


    After that I can get the following result as following:

    So please try to rebuild your project and test it again.

    If it still can not help, please try to post a simple reproduce project in here.
    Besides, if I have misunderstood you, please feel free to let me know.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Wednesday, March 18, 2015 2:52 AM
    Moderator
  • Hi Amy,
    Thank you very much for the help. I have been at this and produced two scenarios where I can output the xml I need on serializing but I cant deserialize and vice versa. Also, it seems serializing manually with xmlserializer and a wcf service dont produce the same results with the same contracts?

    I will create a test case, how do I post code here though?

    Again, thank you so much! 
    Wednesday, March 18, 2015 3:08 AM
  • Hi Ritmo2k,

    Could you please try to post the code in the OneDrive?

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, March 18, 2015 3:28 AM
    Moderator
  • Hi Amy,
    It turns out the default serialization engine in WCF is of course DataContract and my usage outside was all XmlSerializer and this is where the failures had manifested. I figured this out by creating the test case for you:) I need the order independent serialization that XmlSerializer facilitates and so I changed the WCF service over to use it as well and everything works.

    In addition, the Umber class is redundant and I was able to remove that and use XmlArray and XmlArrayItem on a public property named "Umber" in the Indigo class typed as List<Amethyst>.

    Much appreciated again!
    Wednesday, March 18, 2015 4:10 PM