Data Contract Serializer - How to omit the outer element of a collection

前端 未结 3 721
星月不相逢
星月不相逢 2020-11-28 16:13

How do I serialize a list without the outer element using the Data Contract Serializer? I am using .Net 3.5. I have a class that contains a list, amongst

相关标签:
3条回答
  • 2020-11-28 16:27

    The DataContract serializer does not allow this degree of control over the resulted XML, you will have to use instead the XmlSerializer in order to achieve this.

    0 讨论(0)
  • 2020-11-28 16:29

    The below works using MessageContracts although is a 'hack' - it attributes the "MyClass" element to the List member and excludes the wrapper namespace for "MyClass".

    [ServiceContract(Namespace="")]
    public interface IService1
    {
        [OperationContract]
        MyClass GetParameters();
        // TODO: Add your service operations here
    }
    
    [DataContract(Namespace="")]
    public class Parameter
    {
        [DataMember]
        public string ValueName
        {
            get;
            set;
        }
        [DataMember]
        public int Value
        {
            get;
            set;
        }
    
        public Parameter(string ValueName, int Value) 
        { 
            this.ValueName = ValueName; 
            this.Value = Value; 
        } 
    }
    
    [MessageContract(IsWrapped = false, WrapperNamespace="")]
    public class MyClass
    {
        [MessageBodyMember(Name = "MyClass", Namespace = "")]
        public List<Parameter> Parameters
        {
            get;
            set;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 16:33

    Use a collection data contract:

        [CollectionDataContract(Name = "MyClass", ItemName = "Parameter")]
        public class ParameterList : List<Parameter>
        {
    
        }
    

    Here is the actual code:

    public class TestSerialize
    {
        [DataContract(Name = "Parameter")]
        public struct Parameter
        {
            [DataMember(Name = "ValueName")] string ValueName;
            [DataMember(Name = "Value")] int Value;
            public Parameter(string ValueName, int Value)
            {
                this.ValueName = ValueName;
                this.Value = Value;
            }
        }
    
        [CollectionDataContract(Name = "MyClass", ItemName = "Parameter")]
        public class ParameterList : List<Parameter>
        {
    
        }
    
    
        public string Serialize(ParameterList plist)
        {
            var serializer = new DataContractSerializer(plist.GetType());
            var output = new StringBuilder();
            var xmlWriter = XmlWriter.Create(output);
    
            serializer.WriteObject(xmlWriter, plist);
            xmlWriter.Close();
    
            return output.ToString();
        }
    
    
        public void Serialize_produces_2Levels_of_xml()
        {
            ParameterList p = new ParameterList
            {
                new Parameter("First", 1),
                new Parameter("Second", 2),
            };
    
            var xml = Serialize(p);
        }
    }
    

    if you run this you will get the following XML:

    <?xml version="1.0" encoding="utf-16"?>
    <MyClass xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Serialize.Test">
        <Parameter>
            <Value>1</Value>
            <ValueName>First</ValueName>
        </Parameter>
        <Parameter>
            <Value>2</Value>
            <ValueName>Second</ValueName>
        </Parameter>
    </MyClass>
    
    0 讨论(0)
提交回复
热议问题