如何将类属性序列化为xml根的一部分?

时间:2017-05-23 15:32:03

标签: c# xml xml-serialization

我有以下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,但是我收到了错误。

1 个答案:

答案 0 :(得分:1)

如果您想制作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菜单中,选择编辑&gt;选择性粘贴&gt;将XML粘贴为类。生成了一组类。

我在属性[XmlAttribute]TimeStamp之前添加了属性Version。这就是全部,我没有做任何其他改变。

执行此代码:

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成为根元素的属性。