如何将 class 属性 序列化为 xml root 的一部分?

How to serialize a class property to be a part of xml root?

我有以下xml

<OTA_HotelResNotifRS xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Connect.Domain.OTA_2014B.Reservations.OTA_HotelResNotifRS">
    <HotelReservations>
        <HotelReservation>
            <ResGlobalInfo>
                <HotelReservationIDs>
                    <OTA_HotelResNotifRSHotelReservationsHotelReservationResGlobalInfoHotelReservationID>
                        <ResID_Source>some_source</ResID_Source>
                        <ResID_Type>0</ResID_Type>
                        <ResID_Value>51550</ResID_Value>
                    </OTA_HotelResNotifRSHotelReservationsHotelReservationResGlobalInfoHotelReservationID>
                </HotelReservationIDs>
            </ResGlobalInfo>
        </HotelReservation>
    </HotelReservations>
    <Success i:nil="true" />
    <Target i:nil="true" />
    <TimeStamp>0001-01-01T00:00:00</TimeStamp>
    <Version>0</Version>
</OTA_HotelResNotifRS>

使用以下 C# 代码。

    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = 

    "http://www.opentravel.org/OTA/2003/05")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05", IsNullable = false)]
    public partial class OTA_HotelResNotifRS

{   
    /// <remarks/>
    public OTA_HotelResNotifRSHotelReservations HotelReservations
    {
        get; set;
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public System.DateTime TimeStamp
    {
        get; set;
    }
}

如何在根元素中放置时间戳、目标和版本? 我尝试添加 XmlRootAttribute 而不是 XmlAttribute,但出现错误。

如果要制作TimeStampVersion属性,只需添加一个XmlAttribute.

[XmlAttribute]
public DateTime TimeStamp { get; set; }

[XmlAttribute]
public byte Version { get; set; }

要使 Target 属性 为属性,它必须是简单类型,如 intDateTime

[XmlAttribute]
public int Target { get; set; }

如果是复杂类型,如object等,则必须将其保留为元素。

[XmlElement]
public object Target { get; set; }

我把你的xml复制到了剪贴板。

在 Visual Studio 菜单中选择编辑 > 选择性粘贴 > 粘贴 XML 为 类。分别生成了一组类.

我在属性 TimeStampVersion 之前添加了属性 [XmlAttribute]。仅此而已,我没有做任何其他更改。

执行这段代码:

var xs = new XmlSerializer(typeof(OTA_HotelResNotifRS));
OTA_HotelResNotifRS ota;

using (var fs = new FileStream("in.xml", FileMode.Open))
    ota = (OTA_HotelResNotifRS)xs.Deserialize(fs);

using (var fs = new FileStream("out.xml", FileMode.Create))
    xs.Serialize(fs, ota);

最后,我得到了以下xml(为了便于阅读,我对属性进行了格式化并跳过了内部节点):

<?xml version="1.0"?>
<OTA_HotelResNotifRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     TimeStamp="0001-01-01T00:00:00"
                     Version="0"
                     xmlns="http://schemas.datacontract.org/2004/07/Connect.Domain.OTA_2014B.Reservations.OTA_HotelResNotifRS">
  <HotelReservations>
    ...
  </HotelReservations>
  <Success xsi:nil="true" />
  <Target xsi:nil="true" />
</OTA_HotelResNotifRS>

TimeStampVersion 成为根元素的属性。