xml serialization - XML Serializing and Deserializing List<string> in c# -
i have object of following class
[xmlroot("http://schemas.abc.com")] [datacontract] public class employee { [xmlattribute] [datamember] public list<string> positions { get; set; } }
positions contains following 2 strings "project manager" , "senior project manager"
i serialized object through following method , saved in db
public static string serialize(employee entity) { var memorystream = new memorystream(); var serializer = new xmlserializer(typeof(employee)); using (var xmltextwriter = new xmltextwriter(memorystream, encoding.unicode)) { serializer.serialize(xmltextwriter, entity); xmltextwriter.close(); } return unicodebytearraytostring(memorystream.toarray()); } private static string unicodebytearraytostring(byte[] input) { var constructedstring = encoding.unicode.getstring(input); return constructedstring; }
then retrieving , deserializing object through following method
public static employee deserialize(string str) { var data = stringtounicodebytearray(str); var memorystream = new memorystream(data); var serializer = new xmlserializer(typeof(employee)); var xmltextreader = new xmltextreader(memorystream); return (employee)serializer.deserialize(xmltextreader); } private static byte[] stringtounicodebytearray(string input) { var bytearray = encoding.unicode.getbytes(input); return bytearray; }
now getting 5 objects of strings in position list
"project", "manager", "senior", "project", "manager" deserialization creating new string object after every space
quick appreciated
answer eugene podskal
think should remove xmlattribute attribute. xmlserializer doesn't know how parse values in attribute string other splitting on whitespace. or can create property give string allows unambiguously separate individual items in (like commas or whatever), , can parse individual items string set to. propery serializable, while actual list xmlignore
Comments
Post a Comment