Fields marked with ObsoleteAttribute will be ignored by XmlSerializer

來源:互聯網
上載者:User

When I serialized an object into an XML string using XmlSerializer recently,
I found that all the fields which were marked with the ObsoleteAttribute were ignored by XmlSerializer.
I couldn't find the associated attributes in the XML string.
After some tests, I found that XmlSerailzer would ignore these fields both in the serialization process
and the deserialization process, just like they were marked with XmlIgnoreAttributes.
This should be a undocumented feature for .NET.

Let's do some test about this. For example, we have a class called Employee.

public class Employee{  private string _name;  private int _id;  [Obsolete]  [XmlAttribute("name")]  public string Name  {    get { return _name; }    set { _name = value; }  }  [XmlAttribute("id")]  public int ID  {    get { return _id; }    set { _id = value; }  }}private static void Main(){  // Serializes an Employee object into an XML string  Employee e = new Employee();  e.ID = 10;  e.Name = "Employee Name";  XmlSerializer serializer = new XmlSerializer(typeof(Employee));  using (MemoryStream ms = new MemoryStream()) {    TextWriter textWriter = new StreamWriter(ms, Encoding.UTF8);    serializer.Serialize(textWriter, e);    ms.Seek(0, SeekOrigin.Begin);    TextReader textReader = new StreamReader(ms, Encoding.UTF8);    Console.WriteLine(textReader.ReadToEnd());  }}

By making use the code segment above, we can easily serialize an object into an XML string.
If the field "Employee.Name" isn't marked with an ObsoleteAttribute, the serialzied XML looks like:

<?xml version="1.0" encoding="utf-8"?><Employee  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  name="Employee Name" id="10" />

We can also get the serialized XML segment after the field "Employee.Name" is marked with an ObsoleteAttribute.
Then we can find that the "name" attribute of the "Employee" element in the XML segment is missing.
We cannot find it any more. Obviously, the "Employee.Name" property was ignored by XmlSerializer.

So we have known that XmlSerialize will ignore fields which are marked with ObsoleteAttribute when serializing objects.
But what will happen for the deserialization process?

string xml = @"<Employee name=""Employee Name"" id=""10"" />";Employee employee;using (StringReader sr = new StringReader(xml)) {  XmlSerializer serializer = new XmlSerializer(typeof(Employee));  employee = (Employee)serializer.Deserialize(sr);  Assert.IsNotNull(employee);}Console.WriteLine("Employee name: {0}", employee.Name);Console.WriteLine("Employee ID: {0}", employee.ID);

After executing the code segment, we can find that "employee.Name" is empty. That means XmlSerialize also ignored Employee.Name.
The program output is:

Employee name:Employee ID: 10

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.