none
XML attributes instead of elements in data contract serialisation in Rest WCF implementation RRS feed

  • Question

  • I want to uses XML attributes instead of elements, I implemented IXmlSerializable and

    public partial class Id : IXmlSerializable {

    /// <remarks/>
    [XmlAttribute]
    public string lmsId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string unitId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string lmsPresId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string callId;
    
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }
    
    public void ReadXml(System.Xml.XmlReader reader)
    {
        //implement if remote callers are going to pass your object in
    }
    
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("lmsId", lmsId.ToString());
        writer.WriteAttributeString("unitId", unitId.ToString());
        writer.WriteAttributeString("lmsPresId", lmsPresId.ToString());
        writer.WriteAttributeString("callId", callId.ToString());
    }

    }

    But my service help is showing request and response as unknown


    I tried even XmlSerializerFormat public partial class Id : IXmlSerializable {

    /// <remarks/>
    [XmlAttribute]
    public string lmsId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string unitId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string lmsPresId;
    
    /// <remarks/>
    
    [XmlAttribute]
    public string callId;
    
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }
    
    public void ReadXml(System.Xml.XmlReader reader)
    {
        //implement if remote callers are going to pass your object in
    }
    
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("lmsId", lmsId.ToString());
        writer.WriteAttributeString("unitId", unitId.ToString());
        writer.WriteAttributeString("lmsPresId", lmsPresId.ToString());
        writer.WriteAttributeString("callId", callId.ToString());
    }

    }

    then I am getting everything in the xml as xmlElements instead of XmlAttributes But I am getting all the data as elements instead of Xmlattributes

    Tuesday, April 21, 2015 4:23 AM

Answers

  • The Data Contract Serializer used by default in WCF does not support XML attributes. You will have to override the default serializer by applying the [XmlSerializerFormat] attribute to the DataContract. 

    The following is my sample, it will give you the result:

    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Xml.Serialization;
    
    [DataContract]
    [XmlSerializerFormat]
    public class root
    {
       public distance distance=new distance();
    }
    
    [DataContract]
    public class distance
    {
      [DataMember, XmlAttribute]
      public string units="m";
    
      [DataMember, XmlText]
      public int value=1000;
    }

    You can test this with the following code:

    root mc = new root();
    XmlSerializer ser = new XmlSerializer(typeof(root));
    StringWriter sw = new StringWriter();
    ser.Serialize(sw, mc);
    Console.WriteLine(sw.ToString());
    Console.ReadKey();

    The output will be:

    <?xml version="1.0" encoding="utf-16"?>
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                       xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <distance units="m">1000</distance>
    </root>


    I sale myself ONLY half CNY!




    Thursday, April 23, 2015 9:19 AM