Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error DeSerializing XML document - xmlns=''> was not expected

21,495 views
Skip to first unread message

Sunit Joshi

unread,
Mar 29, 2004, 5:53:34 PM3/29/04
to
Hello All
I get this error during DeSerialization. Serialization works fine
though.

Error: There is an error in XML document (2, 2).
<Persons xmlns=''> was not expected.
This is the xml file generated during Serialization...

<?xml version="1.0" encoding="utf-8"?>
<Persons xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person>
<Name>Rick Stacy</Name>
<Address>42. Pine Meadow. #35</Address>
</Person>
<Person>
<Name>Derek Smith</Name>
<Address>23 Ridgeland Drv. #4</Address>
</Person>
</Persons>

Below is the whole class....

using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class Person
{
private string name;
private string address;

public Person()
{}
public Person(string name)
{
this.name = name;
}
[XmlElement]
public string Name
{
get{return name;}
set{name = value;}
}
[XmlElement]
public string Address
{
get{return address;}
set{address = value;}
}
}

public class PersonColl: CollectionBase
{
public PersonColl()
{}
public void Add(Person p)
{
this.List.Add(p);
}
public virtual Person this[int index]
{
get{return (Person)List[index];}
set{List[index] = value;}
}
}

public class Test
{
public static void Main()
{
string[] names = {"Rick Stacy", "Derek Smith"};
string[] addresses = {"42. Pine Meadow. #35", "23 Ridgeland Drv.
#4"};

PersonColl pc = new PersonColl();
for(int i=0; i<names.Length; i++)
{
Person p = new Person();
p.Name = names[i];
p.Address = addresses[i];
pc.Add(p);
}
try
{
TestColl(pc);
SaveXml(pc, "Persons.xml");
GetFromXml("Persons.xml");
}
catch(Exception ex)
{
Console.WriteLine("\nError: " + ex.Message + "\n" +
ex.InnerException.Message);
}
}

public static void TestColl(PersonColl pc)
{
foreach(Person p in pc)
Console.WriteLine(p.Name + "-->" + p.Address);
}

public static void GetFromXml(string filename)
{
XmlTextReader xr = new XmlTextReader(filename);
XmlSerializer xs = new XmlSerializer(typeof(PersonColl));
PersonColl pc = (PersonColl)xs.Deserialize(xr);
Console.WriteLine("Got {0} elements in PersonColl", pc.Count);
}

public static void SaveXml(PersonColl p, string filename)
{
TextWriter tw = new StreamWriter(filename);
XmlRootAttribute xr = new XmlRootAttribute("Persons");
XmlSerializer xs = new XmlSerializer(p.GetType(), xr);
xs.Serialize(tw, p);
tw.Close();
}
}

Christoph Schittko [MVP]

unread,
Mar 29, 2004, 11:32:07 PM3/29/04
to
You need to initialize the XmlSerializer instance the same when you
serialize and deserialize. In your code, you are passing an XmlRootAttribute
to the constructer when you Serialize, but you don't when you deserialize.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Sunit Joshi" <sjo...@ingr.com> wrote in message
news:8f8ffe67.0403...@posting.google.com...

Jiho Han

unread,
Mar 30, 2004, 2:10:23 PM3/30/04
to
This may be what you are looking for. This seems like a pattern that most
people who are not using xml namespace information including myself and I'm
guessing you are not as well.

When you serialize, pass in an instance of XmlSerializerNamespaces with no
namespace:


XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

serializer.Serialize(writer, object, ns);

The resultant xml string will not contain any namespace information and
deserialize without errors. If you are using namespace, then I guess you
should be passing a correct XmlRootAttribute with namespace info which is
missing from your example.

Jiho


"Sunit Joshi" <sjo...@ingr.com> wrote in message
news:8f8ffe67.0403...@posting.google.com...

DotNetJunkies User

unread,
Jun 3, 2004, 5:33:51 PM6/3/04
to
Try this ...

using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace teste
{
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="",
IsNullable=false)]
public class Person
{
[System.Xml.Serialization.XmlElementAttribute(
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name;

[System.Xml.Serialization.XmlElementAttribute(
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string address;

public Person()
{}

public Person(string name)
{
this.name = name;
}

[System.Xml.Serialization.XmlElement("Name",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]


public string Name
{
get{return name;}
set{name = value;}
}

[System.Xml.Serialization.XmlElement("Address",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]


public string Address
{
get{return address;}
set{address = value;}
}
}

public class PersonColl: CollectionBase
{
public PersonColl()
{}
public void Add(Person p)
{
this.List.Add(p);
}

public virtual Person this[int index]
{
get{return (Person)List[index];}
set{List[index] = value;}
}
}

public class Test
{
public static void Main()
{
string[] names = {"Rick Stacy", "Derek Smith"};
string[] addresses = {"42. Pine Meadow. #35", "23 Ridgeland Drv.#4"};

PersonColl pc = new PersonColl();
for(int i=0; i<names.Length; i++)
{
Person p = new Person();
p.Name = names[i];
p.Address = addresses[i];
pc.Add(p);
}

try
{
TestColl(pc);
// SaveXml(pc, "Persons.xml");
// GetFromXml("Persons.xml");
SaveXml( pc, "Persons.xml" );
GetFromXml( "Persons.xml", typeof(PersonColl) );

}
catch(Exception ex)
{
Console.WriteLine("\nError: " + ex.Message + "\n" +
ex.InnerException.Message);
}
}

public static void TestColl(PersonColl pc)
{
foreach(Person p in pc)
Console.WriteLine(p.Name + "-->" + p.Address);
}

public static void GetFromXml(string filename)
{

XmlTextReader xr = new XmlTextReader("c:\\" + filename);


XmlSerializer xs = new XmlSerializer(typeof(PersonColl));
PersonColl pc = (PersonColl)xs.Deserialize(xr);
Console.WriteLine("Got {0} elements in PersonColl", pc.Count);
}

public static void SaveXml(PersonColl p, string filename)
{

TextWriter tw = new StreamWriter("c:\\" + filename);
//XmlRootAttribute xr = new XmlRootAttribute("Persons");


XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");

//XmlSerializer xs = new XmlSerializer(p.GetType(), xr);
XmlSerializer xs = new XmlSerializer(p.GetType());
xs.Serialize(tw, p, ns);
tw.Close();
}

public static void SaveXml(object p, string filename)
{
TextWriter tw = new StreamWriter("c:\\" + filename);


XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

ns.Add("", "");
XmlSerializer xs = new XmlSerializer(p.GetType());
xs.Serialize(tw, p, ns);
tw.Close();
}

public static void GetFromXml(string filename, Type t)
{
XmlTextReader xr = new XmlTextReader("c:\\" + filename);
XmlSerializer xs = new XmlSerializer(t);
object pc = xs.Deserialize(xr);
Console.WriteLine("Got {0} elements in PersonColl", pc.GetType().ToString());
}
}
}

---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching.

carlo...@-NOSPAM-hotmail.com

unread,
Jun 3, 2004, 5:39:25 PM6/3/04
to
try this...

using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace teste


{
/// <remarks/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="",
IsNullable=false)]
public class Person
{
[System.Xml.Serialization.XmlElementAttribute(
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string name;

[System.Xml.Serialization.XmlElementAttribute(
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

public string address;

public Person()
{}

public Person(string name)
{
this.name = name;
}

[System.Xml.Serialization.XmlElement("Name",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]


public string Name
{
get{return name;}
set{name = value;}
}

[System.Xml.Serialization.XmlElement("Address",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]


public string Address
{
get{return address;}
set{address = value;}
}
}

public class PersonColl: CollectionBase
{
public PersonColl()
{}
public void Add(Person p)
{
this.List.Add(p);
}

public virtual Person this[int index]
{
get{return (Person)List[index];}
set{List[index] = value;}
}
}

public class Test
{
public static void Main()
{
string[] names = {"Rick Stacy", "Derek Smith"};
string[] addresses = {"42. Pine Meadow. #35", "23 Ridgeland Drv.#4"};

PersonColl pc = new PersonColl();
for(int i=0; i<names.Length; i++)
{
Person p = new Person();
p.Name = names[i];
p.Address = addresses[i];
pc.Add(p);
}

try
{
TestColl(pc);


// SaveXml(pc, "Persons.xml");
// GetFromXml("Persons.xml");
SaveXml( pc, "Persons.xml" );

GetFromXml( "Persons.xml", typeof(PersonColl) );

}
catch(Exception ex)
{
Console.WriteLine("\nError: " + ex.Message + "\n" +
ex.InnerException.Message);
}
}

public static void TestColl(PersonColl pc)
{
foreach(Person p in pc)
Console.WriteLine(p.Name + "-->" + p.Address);
}

public static void GetFromXml(string filename)
{

XmlTextReader xr = new XmlTextReader("c:\\" + filename);


XmlSerializer xs = new XmlSerializer(typeof(PersonColl));
PersonColl pc = (PersonColl)xs.Deserialize(xr);
Console.WriteLine("Got {0} elements in PersonColl", pc.Count);
}

public static void SaveXml(PersonColl p, string filename)
{

SqlJunkies User

unread,
Nov 5, 2004, 6:01:35 PM11/5/04
to
This seems to be the fix. I was getting the same while Deserializing a DataSet. I used this approach and it works beautifully now.
Thank you.

---
Posted using Wimdows.net NntpNews Component -

Post Made from http://www.SqlJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching.

0 new messages