Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am newbee to c# and trying to execute the below code but getting error as
"There is an error in XML document (2, 2)." while doing Deserilization.

Please suggest the change to resolve the error.

C#
namespace XML_Test
{

    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            t.Foo = "TEST";
            t.Privstring = "Another String";

            XmlSerializer ser = new XmlSerializer(typeof(Test));

            FileStream fs = new FileStream("C:\\Pat\\test.xml", FileMode.OpenOrCreate);

            ser.Serialize(fs,t);

            fs.Close();

            XmlSerializer ser2 = new XmlSerializer(typeof(Test2));

            FileStream instream = new FileStream("C:\\Pat\\test.xml", FileMode.Open);

            //if (ser.Deserialize(instream))
         
                Test2 deserialize = (Test2)ser2.Deserialize(instream);
            

            //Console.Read();

          

        }
    }
    [Serializable]
    public class Test
    {
        public string Foo;
        public string Privstring;
    }
    [Serializable]
    public class Test2
    {
        public string Foo;
        public string Privstring;
    }
}
Posted
Comments
AmitGajjar 27-Dec-11 1:57am    
can you show us your xml file?

I think there is an error in XML document, probably in the second position of the second line. If you could show this fragment of the file, we would be able to see it. :-)

By the way, did you ever hear any jokes about Sherlock Holmes and Dr. Watson? Same thing…

—SA
 
Share this answer
 
The problem is that you make deserialize to another type of data (from Test1 -> Test2).

Try this:
C#
Test1 deserialize = (Test1)ser.Deserialize(instream);

instead of
C#
Test2 deserialize = (Test2)ser2.Deserialize(instream);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900