1

I am writing a WCF service that is supposed to return a following structure:

<trips>  
  <trip attribute1="" attribute2="">  
    <stops>  
      <stop attribute1="" attribute2="">  
          <element1/>  
          <element2>  
       </stop>  
     </stops>  
</trip>

The class looks like following:

[XmlTypeAttribute(AnonymousType = true)]
[DataContract(Name = "Trip")]
public class tripStructure
{
    /// <remarks/>
    [DataMember(Order = 0)]
    public string Number { get; set; }

    /// <remarks/>
    //[XmlAttribute]
    [DataMember(Order = 1)]
    public string Destination { get; set; }

    /// <remarks/>
    //[XmlAttribute]
    [DataMember(Order = 2)]
    public double Longitude { get; set; }

    /// <remarks/>
    //[XmlAttribute]
    [DataMember(Order = 3)]
    public double Latitude { get; set; }

    /// <remarks/>
    //[XmlAttribute]
    [DataMember(Order = 4)]
    public tripStructureStatus Status { get; set; }

    /// <remarks/>
    //[XmlAttribute]
    [DataMember(Name = "UpdateTime", Order = 5)]
    public DateTime TimeStamp { get; set; }

    /// <remarks/>
    [XmlArrayItem("Stop", IsNullable = false)]
    [DataMember(Order = 6)]
    public List<tripStructureStop> Stops { get; set; }
}

Class tripStructureStop is serializeble as well.

If XmlAttribute is commented out the result is following:

<Trips>  
 <Trip>  
  <Number>E432</Number>  
  <Destination>UN</Destination>
  <Longitude>-79.4992</Longitude>
  <Latitude>43.6153</Latitude>
  <Status>S</Status>
  <UpdateTime>2014-10-06T15:49:15.88</UpdateTime>
  <Stops>
    <Stop>
      <ArrivalTime Scheduled="4:23:00 PM" Computed="4:16:52 PM" Status="E"/>
      <DepartureTime Scheduled="4:30:00 PM" Computed="4:23:52 PM" Status="E"/>
      <Code>UN</Code>
    </Stop>
  </Stops>
 </Trip>
</Trip>

All properties are serialized as XmlElement. However, if I want to make them serialized as attributes and I uncomment [XmlAttribute] attribute, than, the entire property will disappear from XML

Any suggestions?

shumiker
  • 11
  • 1
  • 1
    Please see http://stackoverflow.com/questions/591907/how-can-you-control-net-datacontract-serialization-so-it-uses-xml-attributes-in for an answer. – Michael Gunter Oct 06 '14 at 20:21
  • Thank you for your answer. I had already checked that before posting and it threw me an error because I use webHttp binding (for REST) – shumiker Oct 07 '14 at 17:08
  • Sounds like you're screwed. From the link above: "You can't do this with the DataContractSerializer." – Michael Gunter Oct 07 '14 at 18:47

0 Answers0