none
WCF Serialization, converting from classic Xml Serialization

    Question

  • Hi,

    I have some xml looking like this:

    <?xml version="1.0" encoding="utf-8"?>

    <LabelFile>

      <Label>

        <LabelId>#MSL1</LabelId>

        <Text Language="en-us">Start</Text>

        <Text Language="da-dk">Start</Text>

        <Text Language="sv-se">Starta</Text>

      </Label>

      <Label>

        <LabelId>#MSL2</LabelId>

        <Text Language="en-us">Instructions</Text>

        <Text Language="da-dk">Instruktioner</Text>

        <Text Language="sv-se">Anvisningar</Text>

      </Label>  

    <LabelFile>


    I used to serialize it like this:
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
        public class LabelFile
        {
            [XmlElementAttribute("Label", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public List<LabelFileLabel> Items { get; set; }
        }

        public partial class LabelFileLabelText
        {
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string Language { get; set; }
            [System.Xml.Serialization.XmlTextAttribute()]
            public string Value { get; set; }
        }

        public class LabelFileLabel
        {
            [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public string LabelId { get; set; }
            [XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
            public string Comment { get; set; }
            [XmlElementAttribute("Text", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)]
            public LabelFileLabelText[] Text { get; set; }
        }

    And it worked out fine. Now I would like to convert this code into WCF serialization and I have done this:

    [DataContract(Namespace = "")]
        internal class LabelFile
        {
            [DataMember(Name = "Label")]
            public List<LabelFileLabel> Items { get; set; }
        }

        [DataContract]
        public partial class LabelFileLabelText
        {
            [DataMember]
            public string Language { get; set; }
            [DataMember]
            public string Value { get; set; }
        }

        [DataContract]
        public class LabelFileLabel
        {
            [DataMember]
            public string LabelId { get; set; }
            [DataMember]
            public string Comment { get; set; }
            [DataMember]
            public LabelFileLabelText[] Text { get; set; }
        }

    The problem is that the Items property of the LabelFile contains 0 elements.

    Any ideas?

    Thanks
    Henrik Skak Pedersen

    Saturday, June 26, 2010 7:23 AM

Answers

  • Hello,

    if you want to achieve same XML format you have to use XmlSerializer with previous implementation of your data objects. DataContract serializer provides only limited control of resulting XML. As Haripraghash mentioned DataContractSerializer does not allow serializing property to XML attribute and I guess LabelFileLabelText array will also cause problem because collection will be wrapped by another element.

    To use XmlSerializer mark your service or operation contract with [XmlSerializerFormat()] attribute. Than you can control data object serialization with common Xml... attributes.

    Best regards,
    Ladislav

    Saturday, June 26, 2010 10:26 AM

All replies

  • Hi,

    You cannot add xml attributes using datacontract. You have to either use xml serializer or implement ISerializable.


    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Proposed as answer by Haripraghash Tuesday, June 29, 2010 2:12 AM
    Saturday, June 26, 2010 8:58 AM
  • Hello,

    if you want to achieve same XML format you have to use XmlSerializer with previous implementation of your data objects. DataContract serializer provides only limited control of resulting XML. As Haripraghash mentioned DataContractSerializer does not allow serializing property to XML attribute and I guess LabelFileLabelText array will also cause problem because collection will be wrapped by another element.

    To use XmlSerializer mark your service or operation contract with [XmlSerializerFormat()] attribute. Than you can control data object serialization with common Xml... attributes.

    Best regards,
    Ladislav

    Saturday, June 26, 2010 10:26 AM