Monday, 15 June 2015

c# - unable to deserialize all elements of this xml -



c# - unable to deserialize all elements of this xml -

few elements of xml not deserialize , not throw errors either.

<?xml version="1.0" encoding="utf-8"?> <trialmwordsrecord xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <mylist> <mwords> <id>0</id> <name>listmwords1</name> <type>list</type> <myoutput>true</myoutput> <wordselements> <words> <name>listmwords1</name> <value>apple</value> <type>string</type> </words> <words> <name>listmwords1</name> <value>mango</value> <type>string</type> </words> <words> <name>listmwords1</name> <value>chickoo</value> <type>string</type> </words> </wordselements> </mwords> <mwords> <id>1</id> <type>random</type> <myoutput>true</myoutput> <wordselements> <name>limit</name> <value>3,8</value> <type>numeric</type> </wordselements> </mwords> </trialmwordslist> </mylistrecord>

below classes:

[serializable()] [xmlrootattribute("mylistrecord")] public class mylist { [xmlarray("mylist")] public list<mwords> mwords { get; set; } public static mylist deserialize() { xmlserializer deserializer = new xmlserializer(typeof(mylist)); textreader textreader = new streamreader(application.startuppath + "\\mylist.xml"); mylist resultlist = (mylist)deserializer.deserialize(textreader); textreader.close(); homecoming resultlist; } } [serializable()] public class mwords { public int id { get; set; } public mwordstype type { get; set; } public bool myoutput { get; set; } public string requirement { get; set; } [xmlarrayitem("wordselements")] public list<words> wordlist { get; set; } public static mwords deserialize() { xmlserializer deserializer = new xmlserializer(typeof(mwords)); textreader textreader = new streamreader(application.startuppath + "\\mwords.xml"); mwords mwords = (mwords)deserializer.deserialize(textreader); textreader.close(); homecoming mwords; } } public class words { public string value { get; set; } public type type { get; set; } public string name { get; set; } }

now when deserialize xml, if type list, wordlist gets updated, e.g. here count wordlist 3 if type random, wordlist 0 should 1 , not 0. don't know reason.

the problem in xml. @ it's working case:

<wordselements> <words> <name>listmwords1</name> <value>apple</value> <type>string</type> </words> <words> ... </words> <words> ... </words> </wordselements>

now compare broken case:

<wordselements> <name>limit</name> <value>3,8</value> <type>numeric</type> </wordselements>

you've got no words element in there - should be:

<wordselements> <words> <name>limit</name> <value>3,8</value> <type>numeric</type> </words> </wordselements>

if can't alter xml, may need deserialize hand - wouldn't hard.

as aside, might want writing automatically implemented properties on 1 line - it's lot more compact write

public string name { get; set; }

than

public string name { get; set; }

... , it's no harder read, imo.

c# xml xml-serialization deserialization

No comments:

Post a Comment