c# - Read-only property ignored by XmlDocument -


i have model properties:

public class invoice {     public string invoicenumber { get; set; }      [notmapped]     public string title     {                 {             string title = "";             //some algorithm             return title;         }     } } 

my model has 2 properties: 1 of them read-only (title) it's generated programmatically.

i'm generating xmldocument model (generic approach):

private xmldocument generatexmldocument() {     xmldocument xmldocument = new xmldocument();     xmlserializer xmlserializer = new xmlserializer(_objecttoserialize.gettype());     using (memorystream xmlstream = new memorystream())     {         xmlserializer.serialize(xmlstream, _objecttoserialize);         xmlstream.position = 0;         xmldocument.load(xmlstream);     }      //set namespace     xmldocument.documentelement.setattribute("xmlns", xmlnamespace);      return xmldocument; } 

however seems read-only property isn't read generatexmldocument. how solve issue?

the xmlserializer not serialize readonly properties. limitation. should serialize field "title" anyways. use datacontractserializer. more powerful , allows serialize fields using within getter.

see: https://msdn.microsoft.com/en-us/library/mt693218.aspx


Comments