我正在尝试序列化DataContract ..... 我想重命名DataMember。
这是我的DataContract
[DataContract(Name = "Sample")]
[Serializable]
public struct Sample
{
public string CompanyName;
public string AddressLine;
[DataMember(Name="AddressLineRename")]
public string AddressLine2;
public string City;
}
它被序列化为:
<Sample>
<CompanyName>aaa</CompanyName>
<AddressLine>16 aaaa</AddressLine>
<AddressLine2>Unit 66</AddressLine2>
<City>Houston</City>
</Sample>
我需要的是:
<Sample>
<CompanyName>aaa</CompanyName>
<AddressLine>16 aaaa</AddressLine>
<AddressLineRename>Unit 66</AddressLineRename>
<City>Houston</City>
</Sample>
我想将“AddressLine2”序列化为“AddressLineRename”。
Thanks.
答案 0 :(得分:1)
解决了重命名DataMember的问题。
[DataMember(Order = 2, Name = "AddressLineRename", IsRequired = true)]
[XmlElement("AddressLineRename")]
public string AddressLine2;
答案 1 :(得分:0)
您可能需要实施ISerializable
来自行自定义序列化过程。
我认为您需要添加的代码如下所示:
public Sample(SerializationInfo info, StreamingContext context)
{
CompanyName = info.GetString("CompanyName");
// ...
AddressLine2 = info.GetString("AddressLineRename");
// ...
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("CompanyName", CompanyName);
// ...
info.AddValue("AddressLineRename", AddressLine2);
// ...
}
答案 2 :(得分:0)
在我的情况下,我解决了只是将订单参数添加到DataMember
[DataMember(Order = 2, Name = "AddressLineRename")]
public string AddressLine2;
不知道为什么......