c# parse xml array with attribute -
i'm trying parse xml array, has attribute in weird place.
here my(found somewhere on stackowerflow) parser code:
public static string serialize (object objecttoserialize) { memorystream mem = new memorystream (); xmlserializer ser = new xmlserializer (objecttoserialize.gettype ()); ser.serialize (mem, objecttoserialize); utf8encoding utf = new utf8encoding (); return utf.getstring (mem.toarray ()); } public static t deserialize<t> (string xmlstring) { byte[] bytes = encoding.utf8.getbytes (xmlstring); memorystream mem = new memorystream (bytes); xmlserializer ser = new xmlserializer (typeof(t)); return (t)ser.deserialize (mem); }
this works in cases(that need), except here:
<hr> <type n="easy"> <scores> <country name="usa"> <score nickname="steve" rank="1">1982</score> <score nickname="bill" rank="2">1978</score> ...
this template class:
public class countrylist { public countrylist () { } [xmlarray("country")] [xmlarrayitem("score")] public scoreandrank[] country; [xmlattribute("name")] public string name; } }
the score array parsed correctly, "name" null. can value of n out, (as [xmlattribute("n")]). tips how solve this?
edit:solution based on svein fidjestøl's link
[xmltype("country")] public class scorecontainer { public scorecontainer () { } [xmlelement("score")] public scoreandrank[] country; [xmlattribute("name")] public string name; }
works charm.
in order add xml attributes xml arrays, need declare class array element.
a more detailed answer can found here: how add attribute xmlarray element (xml serialization)?
Comments
Post a Comment