472,127 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

XMLSerializer - There was an error generating the XML document.

When I use the XML Serializer in the following example, the first Serialize (with the class Pets) works, but the second (with the cplaa Pets2) fails. Which arrtubute nedds to be applied to make the second Serialize work
namespace SerializeTes

public class Class

static void Main(string[] args

Test _test = new Test()
public class Tes

public void TestAnimal(

Pets newPets = new Pets()

newPets.MyCat = new Cat()
newPets.MyCat.Claws = "shapr"
newPets.MyCat.Name = "Fluffy"
newPets.MyCat.Weight = 5

newPets.MyDog = new Dog()
newPets.MyDog.Trimmed = true
newPets.MyDog.Name = "Spot"
newPets.MyDog.Weight = 10

Pets2 newPets2 = new Pets2()
newPets2.MyPet = new Cat()
((Cat)newPets2.MyPet).Claws = "Trimmed"
((Cat)newPets2.MyPet).Name = ""
((Cat)newPets2.MyPet).Weight = 6

//This will serializ
XmlTextWriter xtw = new XmlTextWriter(@"E:\Pets.xml", Encoding.UTF8)
XmlSerializer xs = new XmlSerializer(typeof(Pets))
xs.Serialize(xtw, newPets)

//This will fail with the following messag
//An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dl
//Additional information: There was an error generating the XML document
XmlTextWriter xtw2 = new XmlTextWriter(@"E:\Pets2.xml", Encoding.UTF8)
XmlSerializer xs2 = new XmlSerializer(typeof(Pets2))
xs.Serialize(xtw2, newPets2)

public abstract class Anima

private string _name
private int _weight

public string Nam


return _name


_name = value
public int Weigh


return _weight


_weight = value

public class Cat : Anima

private string _claws

public string Claw


return _claws


_claws = value

public class Dog : Anima

private bool _trimmed

public bool Trimme


return _trimmed


_trimmed = value

public class Pet

private Cat _myCat
private Dog _myDog

public Cat MyCa


return _myCat


_myCat = value

public Dog MyDo


return _myDog


_myDog = value

public class Pets

private Animal _myPet

public Animal MyPe


return _myPet


_myPet = value

Nov 12 '05 #1
0 9591

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steve Long | last post: by
reply views Thread by Chuck Bowling | last post: by
reply views Thread by William Stacey [MVP] | last post: by
1 post views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.