C# XML Serialization for Silverlight

kiralex

New Member
I'm trying to send an object from my ASP .NET C# MVC3 project to a Silverlight project within the same solution.This is the code I'm using to serialize and deserialize, I got it from CodeProject (http://www.codeproject.com/Articles/233914/Passing-Objects-between-ASP-NET-and-Silverlight-Co)\[code\] public string getSerializedGameData() { Game g = Game.populateByID(this.gameID); MemoryStream mem = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(Game)); mem.Seek(0, SeekOrigin.Begin); xs.Serialize(mem, g); byte[] data = http://stackoverflow.com/questions/11183274/mem.GetBuffer(); return Encoding.UTF8.GetString(data, 0, data.Length); } public static Game GetDeserializedGameObject(string xmlData) { StringReader sr = new StringReader(xmlData); XmlSerializer xs = new XmlSerializer(typeof(Game)); return (Game)xs.Deserialize(sr); }\[/code\]That's the serialization and de-serialization which seems to work.In my ASPX page I've put:\[code\]<input type="hidden" id="game" value="http://stackoverflow.com/questions/11183274/<%=HttpUtility.HtmlEncode(Game.populateByID(Convert.ToInt32(Request["gameID"])).getSerializedGameData()) %>" />\[/code\]Which should get the object, build it into a string, and encode it for HTML. It's then embedded as a hidden field.To decode it I'm using:\[code\] string s = HttpUtility.HtmlDecode(HtmlPage.Document.GetElementById("game").GetProperty("value").ToString()); Game g = Game.GetDeserializedGameObject(s);\[/code\]Now when I run I get the error:\[code\]Data at the root level is invalid. Line 443, position 8\[/code\]Looking at the data I see valid XML until...\[code\]... </gameEvents></Game>???????????\[/code\]Except there's thousands of the invalid characters, I deleted them to keep it short.Looking at the source I see:\[code\]... </gameEvents></Game>" />\[/code\]So it doesn't appear to be an encoding issue.Originally I believed the extra characters were excess from the data buffer, but I don't see it coming across in the source, and if the XML Serialize and Deseriailize has been tested, that leaves the HTMLDecode..but I can't spot anything wrong with it.I realize I could probably strip off everything after the last > but I'd like to know what's causing it, as I shouldn't have to do that.Any help would be appreciated, thanks!
 
Back
Top