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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:55:14+00:00 2026-05-26T10:55:14+00:00

How do I serialize and deserialize multiple objects using DataContractSerializer? Serializing is fine, however

  • 0

How do I serialize and deserialize multiple objects using DataContractSerializer?
Serializing is fine, however during deserialization I get the error

“The serialization operation failed. Reason: There was an error deserializing the object of type Serialization.Person. There are multiple root elements.”

The error message clearly mentions that there is no root element to the serialized document.
But how do i overcome this?

Here’s the code:

[DataContract]
class Person {
    [DataMember(Name = "CustName")]
    internal string Name;
    public Person(string n) {Name = n;}
}

class Program {
    public static void Main() {
            WriteObject("d:\\temp\\DataContractExample.xml" , "Mary");
            WriteObject("d:\\temp\\DataContractExample.xml", "Joe");
            ReadObject("d:\\temp\\DataContractExample.xml");
    }

    public static void WriteObject(string path, string name) {
        Person p1 = new Person(name);
        FileStream fs = new FileStream(path, FileMode.Append);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        DataContractSerializer ser =  new DataContractSerializer(typeof(Person));
        ser.WriteObject(writer, p1);

        writer.Close();
        fs.Close();
    }
    public static void ReadObject(string path) {
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
        XmlDictionaryReader reader =
            XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

        DataContractSerializer ser = new DataContractSerializer(typeof(Person));

        // Deserialize the data and read it from the instance.
        Person[] newPerson = (Person[])ser.ReadObject(reader);
        Console.WriteLine("Reading this object:");
        Console.WriteLine(String.Format("{0}", newPerson[0].Name));
        fs.Close();
    }

When I read from the DataContractSerializer, ser.ReadObject(reader), I get the exception that I mentioned above.
Is it possible to Create root element while storing multiple objects using DataContractSerializer?

  • 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-26T10:55:14+00:00Added an answer on May 26, 2026 at 10:55 am

    DataContractSerializer works on xml documents, so expects a single top-level element. The simplest approach would be to serialize a List<Person>, which should avoid this. You could also add an outer element manually, perhaps using XmlReader and ReadSubtree during serialization (although note: this is ugly hard work).

    The simplest option, though, is to simply serialize a List<Person> from the outset, and deserialize as a List<Person> – this will then be a single xml hunk, so won’t upset the deserializer.

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

Sidebar

Related Questions

I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at
I'm currently convering my ASP.NET v2 application to serialize/deserialize it's objects because I want
I would like to serialize and deserialize objects without having to worry about the
Is it possible to serialize and deserialize a class in C++? I've been using
I have a class that I serialize/deserialize using XmlSerializer . This class contains a
Is it possible to override the default WCF DataContractSerializer behaviour when Serialize/DeSerialize entities and
We serialize/deserialize XML using XStream... and just got an OutOfMemory exception. Firstly I don't
I am trying to serialize/deserialize objects that have factory-created members. For example, lets say
OK, so here's the story so far. I could already deserialize individual objects using
I am using two methods below to serialize/deserialize entity framework object (ver. 4.0). 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.