C# 如何将类属性序列化为xml根的一部分?
我有以下xmlC# 如何将类属性序列化为xml根的一部分?,c#,xml,xml-serialization,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> <HotelReservati
<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>
某些来源
0
51550
0001-01-01T00:00:00
0
使用以下c#代码
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=
"http://www.opentravel.org/OTA/2003/05")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://www.opentravel.org/OTA/2003/05“,IsNullable=false)]
公共部分类奥塔尤酒店
{
///
公共奥塔尤酒店Resnotifrs酒店预订
{
获得;设置;
}
///
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime时间戳
{
获得;设置;
}
}
如何在根元素中放置时间戳、目标和版本?
我试图添加XmlRootAttribute而不是XmlAttribute,但出现了一个错误。如果要创建
时间戳
和版本
属性,只需添加XmlAttribute
[XmlAttribute]
public DateTime TimeStamp { get; set; }
[XmlAttribute]
public byte Version { get; set; }
要将Target
属性作为属性,它必须是一个简单类型,如int
、DateTime
等
[XmlAttribute]
public int Target { get; set; }
如果它是复杂类型,如对象
等,则必须将其保留为元素
[XmlElement]
public object Target { get; set; }
我已将您的xml复制到剪贴板 在VisualStudio菜单中,选择编辑>粘贴特殊内容>将XML粘贴为类。生成了一组类 我在属性
时间戳
和版本
之前添加了属性[xmldattribute]
。仅此而已,我没有做任何其他更改
执行以下代码:
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(我格式化了属性以便于阅读,并跳过了内部节点):
...
TimeStamp
和Version
成为根元素的属性。添加XmlAttribute不会更改xml输出。例如,时间戳仍然没有定位在根目录中……您可以粘贴您的代码吗,因为无论我做什么,我总是得到相同的结果。它与XmlFormatter配置有关吗?对我来说也适用,但当我将它作为HttpResponseMessage的一部分使用时,即Request.CreateResponse(HttpStatusCode.OK,object,configuration.Formatters.XmlFormatter)我弄错了result@mko-默认情况下,WebAPI使用DataContractSerializer
,它不使用XmlAttribute
。设置Configuration.Formatters.XmlFormatter.UseXmlSerializer=true代码>
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 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>