c# - Deserialize xml into class having string values in xml -
let me explain, there database table has 1 xml column named audits , other common types of column.
so possible deserialize below xml class.
<?xml version="1.0"?> <entity type="order"> <id type="system.int64">146</id> <ordernumber type="system.string">od555</ordernumber> <audits type='system.string'> <audit> <item> <create timestamp='2017-07-19 10:02:13' userid='23' /> </item> <invoice> <create timestamp='2017-07-19 10:03:37' userid='45' /> </invoice> </audit> </audits> </entity>
class:
public class order { public long id { get; set; } public string ordernumber { get; set; } public string audits { get; set; } }
modifying model attributes xmltype
, xmlanyelement
(requires xmlelement
type)
[xmltype("entity")] public class order { public long id { get; set; } public string ordernumber { get; set; } [xmlanyelement] public xmlelement audits { get; set; } }
allows deserialize complete xml string like
using (memorystream stream = new memorystream()) using (streamwriter writer = new streamwriter(stream)) { writer.write(xmlstring); writer.flush(); stream.position = 0; xmlserializer serializer = new xmlserializer(typeof(order)); order o = (order)serializer.deserialize(stream); }
now able audits
string like
string auditsstring = o.audits.innerxml;
you can add property model simplify access:
public string auditsstring { { return audits.innerxml; } }
Comments
Post a Comment