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 4051164
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:09:38+00:00 2026-05-20T14:09:38+00:00

All, I am trying to serialize and de-serialize a class and the de-serialization is

  • 0

All, I am trying to serialize and de-serialize a class and the de-serialization is failing. There are tons of similar threads, but I am unable to resolve this. I am getting the following error “There is an error in XML document (2, 2)” Inner expection “{” was not expected.”}”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace XMLSerialization
{
 [System.Xml.Serialization.XmlRootAttribute(Namespace = "",
     IsNullable = false)] 
 public class Level
 {
  private String _name;
  private String _background;

  public Level() 
  {
   _name = "LEVEL_NAME";
   _background = "LEVEL_BACKGROUND_IMAGE";
  }

  [XmlAttribute("LevelName")]
  public String LevelName
  {
   get { return _name; }
   set { _name = value; }
  }

  [XmlAttribute("Background")]
  public String Background
  {
   get { return _background; }
   set { _background = value; }
  }
 }
}

This is the code I use for de-serialization. The serialization is happening fine but the de-serialization is not going through. I think that I am doing a trivial mistake but I am unable to solve this!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace XMLSerialization
{
 class Program
 {
  static void Main(string[] args)
  {
   Level oLevel1 = new Level();

   XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
   ns.Add("", ""); 
   XmlSerializer serializer = new XmlSerializer(typeof(Level));
   TextWriter textWriter = new StreamWriter("Level1.xml");
   serializer.Serialize(textWriter, oLevel1, ns);
   textWriter.Close();

   Level oLevel2 = new Level();
   XmlSerializer deserializer = new XmlSerializer(typeof(List<Level>));
   TextReader textReader = new StreamReader("Level1.xml");
   oLevel2 = (Level)deserializer.Deserialize(textReader);
   textReader.Close();
  }
 }
}
  • 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-20T14:09:39+00:00Added an answer on May 20, 2026 at 2:09 pm

    I think you need to change the line

    XmlSerializer deserializer = new XmlSerializer(typeof(List<Level>));
    

    To

    XmlSerializer deserializer = new XmlSerializer(typeof(Level));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to XML serialize a class object but having the following problem:
I'm having this very odd issue while trying to serialize a class for logging
I may be way off base, but I've been trying all afternoon to run
I'm trying to build an object that looks something like this: public class MyObject
I'm trying to serialize class B as an instance of ita base class A
I am trying to serialize a list of descendants. This is what I have
I'm trying to serialize the main class in my VB solution. I've added the
I'm trying to build a Lucene Serializer class that would serialize/de-serialize objects (classes) with
I'm trying to add XML serialization to a fairly trivial class structure in C#.
I'm trying to serialize all of my objects when the user clicks on the

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.