我的程序的一个工作是从xml文件中读取客户列表并将它们反序列化为C#类,如下所示: ?xml version="1.0" encoding="utf-8"?customers customer namejohn/name id1/id /customer customer namemike/name id2/id /customer/cu
<?xml version="1.0" encoding="utf-8"?> <customers> <customer> <name>john</name> <id>1</id> </customer> <customer> <name>mike</name> <id>2</id> </customer> </customers>
C#类:
[XmlRoot("customers")] public class CustomerList { [XmlElement("customer")] public Customer[] Customers { get; set; } } public class Customer { [XmlElement("name")] public String Name {get; set;} [XmlElement("id")] public String Id {get; set;} }
但最近客户想要从< id>更改标签名称到< code>如下所示:
<?xml version="1.0" encoding="utf-8"?> <customers> <customer> <name>john</name> <code>1</code> </customer> <customer> <name>mike</name> <code>2</code> </customer> </customers>
“code”的值与之前的标记“id”具有相同的含义.他们希望在转换过程中修改程序,以便在一段时间内识别这两个标记.
有没有简单的方法来实现这一目标?谢谢.
为什么不使用一个私有字段并使用两个不同的getter / setter?只要两个标记都没有出现在XML中,这就行了.[XmlRoot("customers")] public class CustomerList { [XmlElement("customer")] public Customer[] Customers { get; set; } } public class Customer { private String _id; [XmlElement("name")] public String Name {get; set;} [XmlElement("id")] public String Id {get{return _id;} set{_id = value;}} [XmlElement("code")] public String Code {get{return _id;} set{_id = value;}} }