Tuesday, 11 October 2016

XML : XML serliazation need Override XML Root and XmlType attributes with derived class

I need to specify in the overrides attributes for derivered class the root attribute to serialize sample class but I can't figure out how solve this problem ...

I saw similar answer to that question and I try different ways but none works..

  public class Payment  {      //abstract methods      public string Prop { get; set; }  }    [Serializable]  public class BankPayment : Payment  {      //method implementations  }    var bankPaymentType = typeof( BankPayment );        var xRoot = new XmlRootAttribute      {          Namespace = "ClassLibrary1",      };        var attributes = new XmlAttributes(){ XmlRoot = xRoot };        attributes.XmlElements.Add( new XmlElementAttribute( bankPaymentType ) );        var overrides = new XmlAttributeOverrides();      overrides.Add( typeof( Payment ), attributes );        var serializer = new XmlSerializer( typeof ( Payment ), overrides );        var pay = new BankPayment();        using ( var writer = new StringWriter() )      {          serializer.Serialize( writer, pay );          Console.WriteLine( writer.ToString() );      }    

I can't specify any attribute in the derived class cause is an external libray

the above code give me this error:

Root XML attributes and XmlType may not have been specified for the type ClassLibrary1.Payment.

Anyone can help me?

thanks in advance..

No comments:

Post a Comment