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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:20:36+00:00 2026-05-21T22:20:36+00:00

I have to first deserialize xml file to List then enlarge this list and

  • 0

I have to first deserialize xml file to List then enlarge this list and serialize all with add 1 object more (magnification is needed for this)

code from XML:

<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
           <name>Danie</name>
           <lastName>McJackie</lastName>
           <age>27</age>
 </Person>

code from .cs file: I commented where i got error and copy the error message

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

public partial class _Default : System.Web.UI.Page 
{
   protected void Page_Load(object sender, EventArgs e)
   {

   }
   private List<Person> Deserialize(string path)
   {
       using (FileStream fs = new FileStream(path, FileMode.Open))
       {
           XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
           return (List<Person>)ser.Deserialize(fs);
           //There is an error in XML document (2, 2).
           // I got this error and don't know how to manage with it.
       }
   }

   protected void Button1_Click(object sender, EventArgs e)
   {
       string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
       List<Person> people = File.Exists(path) ? Deserialize(path) :new List<Person>();
       people.Add(new Person(TextBox1.Text, TextBox2.Text, int.Parse(TextBox3.Text)));
       using (FileStream fs = File.OpenWrite(path))
       {
           XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
           ser.Serialize(fs, people);
       }


   }
}

and at least class to serialize:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;



[Serializable]
public class Person
{

   public string name;
   public string lastName;
   public int age;
   public Person(string _name, string _lastName, int _age)
   {
       name = _name;
       lastName = _lastName;
       age = _age;
   }
   public Person()
   {

   }
}

exception details:

System.InvalidOperationException was unhandled by user code
Message=There is an error in XML document (2, 2).
Source=System.Xml
StackTrace:
      at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,                                             String    encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at _Default.Deserialize(String path) in e:\Programy c#\WebSites\ASP8\Default.aspx.cs:line 22
   at _Default.Button1_Click(Object sender, EventArgs e) in e:\Programy c#\WebSites\ASP8\Default.aspx.cs:line 29
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)                
       at System.Web.UI.Page.ProcessRequestMain(Boolean                                                                includeStagesBeforeAsyncPoint,       Boolean includeStagesAfterAsyncPoint)
  InnerException: System.InvalidOperationException
       Message=<Person xmlns=''> was not expected.
   Source=_1r-cm1p
   StackTrace:
        at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfPerson()
   InnerException: 

This is what i added with last edit

Deserialize file .cs:

string path = Server.MapPath(Request.ApplicationPath + "/test.xml");
    using (FileStream fs = new FileStream(path, FileMode.Open))
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
        List<Person> os = (List<Person>)ser.Deserialize(fs);
        foreach (var i in os)
        {
            Label1.Text += i.Name + "<hr />" + i.LastName + "<hr />" + i.Age.ToString() + "<hr />";
        }

        fs.Close();

everything work fine thanks everyone for help:)

  • 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-21T22:20:37+00:00Added an answer on May 21, 2026 at 10:20 pm

    Instead of

    XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
    

    Try

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

Sidebar

Related Questions

I serialize an XML file into an object using the built-in .NET functionality (XmlSerializer.Deserialize).
My Java application needs to serialize/deserialize an XML structure received via HTTP. This XML
So, deserializing is working however I have some xml like this: <File> <Stuff>stuff</Stuff> <Devices>
OK, first of all of this is done in Java: I deserialize a text
I have got an object serialization from Java into a XML file that I
I have the first few lines of a text file fx.txt with the following
Let's say I have a first structure like this: typedef struct { int ivalue;
I have an Object with (de-)serializes its configuration via system.xml.serializer The config is in
I have some XML that I am trying serialize like so: string Value =
I have written a program which will serialize and de-serialize, it does this fine

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.