Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:42:13+00:00 2026-06-06T09:42:13+00:00

I’m trying to send an object from my ASP .NET C# MVC3 project to

  • 0

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:

...
  &lt;/gameEvents&gt;

&lt;/Game&gt;" />

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:42:14+00:00Added an answer on June 6, 2026 at 9:42 am

    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" }
    ) %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.