473,326 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 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

[STAThread
static void Main(string[] args

Test _test = new Test()
_test.TestAnimal()
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)
xtw.Close()

//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)
xtw2.Close()


[XmlRoot
public abstract class Anima

private string _name
private int _weight

public string Nam

ge

return _name

se

_name = value
public int Weigh

ge

return _weight

se

_weight = value


public class Cat : Anima

private string _claws

public string Claw

ge

return _claws

se

_claws = value


public class Dog : Anima

private bool _trimmed

public bool Trimme

ge

return _trimmed

se

_trimmed = value


public class Pet

private Cat _myCat
private Dog _myDog

public Cat MyCa

ge

return _myCat

se

_myCat = value

public Dog MyDo

ge

return _myDog

se

_myDog = value


public class Pets

private Animal _myPet

public Animal MyPe

ge

return _myPet

se

_myPet = value


Nov 12 '05 #1
0 9697

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
4
by: Steve Long | last post by:
Hello, I hope this is the right group to post this to. I'm trying to serialize a class I've written and I'd like to be able to serialze to both binary and xml formats. Binary serialization is...
0
by: Chuck Bowling | last post by:
I'm not sure what I'm doing wrong here. I'm getting the exception "An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll Additional information: There...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
3
by: Rich S. | last post by:
Hello, I have a C# .NET 2.0 program which accesses the Amazon.com E-Commerce web service. I am debugging it as a ClickOnce Partial Trust application. My question is, What permissions are...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
1
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
How might I serialize an IPAddress? I read it needs to have a default constructor so I made a wrapper class: public class IPAddressEx : IPAddress { public IPAddressEx() : base(...
2
by: christopher.watford | last post by:
I'm loading a plugin assembly using Activator.CreateInstanceFrom, and inside this assembly is a settings class which gets serialized to XML. The general code flow is as follows: ObjectHandle...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.