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

  • Home
  • SEARCH
  • 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 3806000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:46:01+00:00 2026-05-19T14:46:01+00:00

I’m having a strange problem deserializing and was wondering if someone could shed some

  • 0

I’m having a strange problem deserializing and was wondering if someone could shed some light on it. Sorry for the rough code but this was meant as a prototype.

Basically I’m attempting to serialize and deserialize a simple class:

[Serializable]

 [Preserve(AllMembers=true)]

 public class School

 {

  public School ()

  {

  }

   public string est_name{get; set;}

             public string postcode{get; set;}

          public string phase {get; set;}

            public string head_name{get; set;}

          public string urn {get; set;}

          public long distance{get; set;}

           public string coord{get; set;}

           public string gender{get; set;}

          public int totpup{get; set;}

   public int totelig{get; set;}

   public float pmattest14p{get; set;}

   public float pmattest15{get; set;}

      public float pengtest14p{get; set;}

      public float pengtest15{get; set;}

                    public float apsengmattest{get; set;

         }

The code for serializing is as follows, using School[] as T, Its a method from a generic class:

public void serializesample(T sample)

  {

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Sample2.xml")      Stream st = new FileStream(path, FileMode.OpenOrCreate);

        XmlWriter w = new XmlTextWriter(st, Encoding.UTF8);

                  serializer.Serialize(w, sample);   

        st.Flush(); 

                st.Close();  

            }

resulting in the following XML file:

<?xml version="1.0" encoding="utf-8"?>

<ArrayOfSchool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<School><est_name>testName</est_name><postcode>N7 0NA</postcode><head_name>thedude</head_name><distance>0</distance><gender>mixed</gender><totpup>0</totpup><totelig>0</totelig><pmattest14p>0</pmattest14p><pmattest15>0</pmattest15><pengtest14p>5</pengtest14p><pengtest15>3</pengtest15><apsengmattest>0</apsengmattest></School><School><est_name>testName2</est_name><postcode>N7 4NA</postcode><head_name>thedude</head_name><distance>0</distance><gender>mixed2</gender><totpup>0</totpup><totelig>0</totelig><pmattest14p>0</pmattest14p><pmattest15>0</pmattest15><pengtest14p>5</pengtest14p><pengtest15>3</pengtest15><apsengmattest>0</apsengmattest></School></ArrayOfSchool> 

The deserialization code is as follows, with School[] as T:

private T ReadObject(XmlReader reader)
  {

   XmlSerializer serializer = new XmlSerializer(typeof(T));
   return (T)serializer.Deserialize(reader);   
  }

XmlReader r = XmlReader.Create(new     FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Sample2.xml"), FileMode.Open));

e.responseObject = ReadObject(r);

I keep getting the following exception at the last listed line “responseObject = ReadObject(r);”:

System.InvalidOperationException: There is an error in XML document. —> System.InvalidOperationException: (unknown) was not expected
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x00050] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:182
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0001c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:361
— End of inner exception stack trace —
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00061] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:366
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader) [0x0002c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:350
at Burnspeed.Utilities.WebRequestHelper1[SchoolFinder_Prototype.School[]].ReadObject (System.Xml.XmlReader stream) [0x00010] in /Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:201
at Burnspeed.Utilities.WebRequestHelper
1[SchoolFinder_Prototype.School[]].ProcessHttpResponseAndFire (IAsyncResult iar) [0x000ae] in /Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:156

  • 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-05-19T14:46:01+00:00Added an answer on May 19, 2026 at 2:46 pm

    I am not completely sure why this exception happens for you. I tried your code with only small modifications and it works. I didn’t use [Preserve(AllMembers=true)], I replaced every T by a School[] (which you did as well, didn’t you?) and I replaced the line “e.responseObject = ReadObject(r)”, where your exception happens, by “School[] newArray = ReadObject(r)”. Didn’t want to write the rest of the code for not modifying these little bits.

    Instead of replacing every T by a School[], you could also change the methods to

        public void serializesample<T>(T sample) {
    
            XmlSerializer serializer = new XmlSerializer(typeof(T));
    
            string path = "G:\\sample.xml";
            Stream st = new FileStream(path, FileMode.OpenOrCreate);
    
            XmlWriter w = new XmlTextWriter(st, Encoding.UTF8);
    
            serializer.Serialize(w, sample);
    
            st.Flush();
    
            st.Close();
    
        }
    
        private T ReadObject<T>(XmlReader reader) {
    
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(reader);
        }
    

    and call them via serializesample<School[]>(array) and ReadObject<School[]>(r).

    I am just not sure what exactly you did with the T’s in your code and I guess the problem could be somewhere there. Hope that helps, even though it probably won’t help if the exception comes from the Monotouch [Preserve(AllMembers=true)].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.