如果设置了[XmlAttribute],则属性将从XML隐藏

时间:2014-10-06 20:14:41

标签: c# .net wcf xml-serialization xml-attribute

我正在编写一个应该返回以下结构的WCF服务:

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

该课程如下:

[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 也是可序列化的。

如果 XmlAttribute 被注释掉,结果如下:

<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>

所有属性都被序列化为XmlElement。 但是,如果我想将它们序列化为属性并且我取消注释[XmlAttribute]属性,那么整个属性将从XML中消失

有什么建议吗?

0 个答案:

没有答案