Error while deserializing xml string into object using XMLSerializer in C# -
here dto
[xmltypeattribute(typename="xattributes")] public class xattributes { [xmlarray(elementname="attributes")] [xmlarrayitem(elementname="attribute")] public list<attribute> attributes { get; set; } public xattributes() { attributes = new list<attribute>(); } } public class attribute { [xmlelement(elementname = "name")] public string name { get; set; } [xmlelement(elementname = "value")] public string value { get; set; } [xmlelement(elementname = "valuetype")] public valuetype valuetype {get; set; } [xmlarray(elementname="children" )] public list<attribute> children { get; set; } } and here deserializing code
public static t toobject<t>(this string xmldata) { var s = new xmlserializer(typeof(t)); object obj=null; using (var sr = new stringreader(xmldata)) { obj = s.deserialize(sr); } homecoming (t)obj; } and here code phone call deserialize it
string attr = @"<xattributes><attributes> <attribute> <name>test</name> <value>testvlau</value> <valuetype>string</valuetype> <children/> </attribute> <attribute> <name>test1</name> <value>testvlau1</value> <valuetype>string</valuetype> <children/> </attribute> </attributes><xattributes>"; var attribute = attr.toobject<xattributes>(); i error
there error in xml document (14, 55). at line
obj = s.deserialize(sr); any help appreciated.
thanks.
it's hard tell here problem seek following.... replace
[xmltypeattribute(typename="xattributes")] public class xattributes { .... } with this:
[xmlroot("xattributes")] public class xattributes { .... } also, don't forget include xml declaration in xml string:
string attr = @"<?xml version="1.0" encoding="utf-8"?> <xattributes><attributes> <attribute> <name>test</name> <value>testvlau</value> <valuetype>string</valuetype> <children/> </attribute> <attribute> <name>test1</name> <value>testvlau1</value> <valuetype>string</valuetype> <children/> </attribute> </attributes><xattributes>"; ps: have created generic xmlserializer may useful, please take @ article in codeproject:
xml serialization using generics
c# xml xmlserializer
No comments:
Post a Comment