Reputation: 431
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)
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 = 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);
}
That's the serialization and de-serialization which seems to work.
In my ASPX page I've put:
<input type="hidden" id="game" value="<%=HttpUtility.HtmlEncode(Game.populateByID(Convert.ToInt32(Request["gameID"])).getSerializedGameData()) %>" />
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:
string s = HttpUtility.HtmlDecode(HtmlPage.Document.GetElementById("game").GetProperty("value").ToString());
Game g = Game.GetDeserializedGameObject(s);
Now when I run I get the error:
Data at the root level is invalid. Line 443, position 8
Looking at the data I see valid XML until...
...
</gameEvents>
</Game>�����������
Except there's thousands of the invalid characters, I deleted them to keep it short.
Looking at the source I see:
...
</gameEvents>
</Game>" />
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!
Upvotes: 0
Views: 459
Reputation: 1039190
You should use .ToArray()
instead of .GetBuffer()
on the MemoryStream. The buffer could be larger than the actual contents and thus the invalid bytes at the end. Also you should properly dispose IDisposable resources by wrapping them in using
statements:
public string getSerializedGameData()
{
Game g = Game.populateByID(this.gameID);
using (MemoryStream mem = new MemoryStream())
{
XmlSerializer xs = new XmlSerializer(typeof(Game));
xs.Serialize(mem, g);
byte[] data = mem.ToArray();
return Encoding.UTF8.GetString(data, 0, data.Length);
}
}
public static Game GetDeserializedGameObject(string xmlData)
{
using (StringReader sr = new StringReader(xmlData))
{
XmlSerializer xs = new XmlSerializer(typeof(Game));
return (Game)xs.Deserialize(sr);
}
}
Now that we have fixed the server side code, the view also needs fixing. You should HTML helpers in your view which will take care of properly encoding the value in the hidden field:
<%= Html.Hidden(
"game",
Game.populateByID(Convert.ToInt32(Request["gameID"])).getSerializedGameData(),
new { id = "game" }
) %>
Upvotes: 1