序列化/反序列化不同的属性名称? [英] Serialize/Deserialize different property names?
问题描述
我有一个旧系统,在请求信息调用中返回的XML名称如下:
I have an old system which in a request info call returns xml with names that look like:
postalCodeField,firstNameField ...
postalCodeField, firstNameField...
然后,同一系统进行了一个修改调用,该调用采用了如下所示的xml:
That same system then has a modify call which takes xml that looks like:
PostalCode,fistName,lastName...。
PostalCode, fistName, lastName....
有没有一种方法可以构建一个对象,该对象将反序列化请求,但是使用不同的名称序列化xml输出?
Is there a way to build an object that would deserialize the request, yet serialize the xml output with different names?
具体来说:
public class RequestInfo
{
public string firstNameField{get;set;}
public string lastNameField{get;set;}
}
public class ModifyInfo
{
public string firstName{get;set;}
public string lastName{get;set;}
public ModifyInfo(){}
public ModifyInfo(RequestInfo ri)
{
firstName = ri.firstNameField
.....
}
}
有没有一种方法可以说出属性来使这些
Is there a way through say attributes to make these into the same object?
EDIT
有没有办法做到这一点?
Is there a way to have a single object that would accept one tag name on deserialize, then output a different name on serialize?
<单个对象将在反序列化时接受一个标签名称,然后在序列化时输出另一个名称? myTagField />(反序列化为)myObj.MyTag(序列化为)< MyTag />
< myTagField /> (deserialize to) myObj.MyTag (serialize to) < MyTag />
推荐答案
请务必注意您使用的是哪个序列化程序。每个不同的序列化器的工作方式都不同。
It's important to note which actual serializer you're using. Every different serializer works differently.
我假设您正在使用 System.Xml.Serialization.XmlSerializer
。如果是这种情况,那么您想使用 System.Xml.Serialization
命名空间中的属性,例如XmlElementAttribute,例如:
I assume you're using the System.Xml.Serialization.XmlSerializer
. If that is the case, then you want to use the attributes in the System.Xml.Serialization
namespace such as XmlElementAttribute like so:
public class Person
{
[System.Xml.Serialization.XmlElement(ElementName = "firstNameField")]
public string FirstName { get; set; }
}
这假定该字段是XML元素。如果是属性,请使用 XmlAttribute
属性。
This assumes the field is an XML element. If it's an attribute, use the XmlAttribute
attribute.
这篇关于序列化/反序列化不同的属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!