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();
}
}
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Sunit Joshi" <sjo...@ingr.com> wrote in message
news:8f8ffe67.0403...@posting.google.com...
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...
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.
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)
{
---
Posted using Wimdows.net NntpNews Component -
Post Made from http://www.SqlJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching.