473,856 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
_test.TestAnima l()
public class Tes

public void TestAnimal(

Pets newPets = new Pets()

newPets.MyCat = new Cat()
newPets.MyCat.C laws = "shapr"
newPets.MyCat.N ame = "Fluffy"
newPets.MyCat.W eight = 5

newPets.MyDog = new Dog()
newPets.MyDog.T rimmed = true
newPets.MyDog.N ame = "Spot"
newPets.MyDog.W eight = 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.xm l", Encoding.UTF8)
XmlSerializer xs = new XmlSerializer(t ypeof(Pets))
xs.Serialize(xt w, newPets)

//This will fail with the following messag
//An unhandled exception of type 'System.Invalid OperationExcept ion' occurred in system.xml.dl
//Additional information: There was an error generating the XML document
XmlTextWriter xtw2 = new XmlTextWriter(@ "E:\Pets2.x ml", Encoding.UTF8)
XmlSerializer xs2 = new XmlSerializer(t ypeof(Pets2))
xs.Serialize(xt w2, 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 9728

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

Similar topics

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 create duplicate XML representations of the shared object, but instead use IDREFs to refer to the shared object. The XML I'm trying to produce is as follows (where "href" is an IDREF): <?xml version="1.0" encoding="utf-8"?> <MyRootClass...
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 working fine but when I try to instantiate an XmlSerializer object with: Dim xmls As New XmlSerializer(GetType(CLayerDefinition)) I get the following error:
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 was an error generating the XML document." This exception only showed up after I added the ArrayList and populated it with an instance of RepetitionEvent.
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 some digging, I found XmlSerializer chokes on 0x03 chars. It probably chokes on many others, but this one I found. It serializes ok, but chokes on deserialize on "<Field1>&#x3;</Field1>". So the questions are: 1) Why does serializer produce...
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 required for the XMLSerializer.Serialize method? This is where the application constantly bombs with an InvalidOperationException. The app runs without any errors when debugged as "Full Trust." However,
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 schema using xsd.exe, which took a while because xsd doesn't handle recursive elements very well (StackOverFlow exception during generation). Now that I have the classes I am trying to serialize them to an xml document to send to ArcIMS to generate...
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 value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml schema. This all sounds simple, but SQL Server REQUIRES the timezone to be specified for date values....
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( IPAddress.None.GetAddressBytes() )
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 pluginHandle = Activator.CreateInstanceFrom(assemblyName, type); object plugin = pluginHandle.Unwrap(); ....
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.