473,659 Members | 2,996 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

[STAThread
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)
xtw.Close()

//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)
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 9710

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

Similar topics

5
5414
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...
4
1985
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:
0
1073
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.
0
2290
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...
3
1232
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,
8
5446
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...
10
4777
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....
1
4073
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() )
2
5094
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(); ....
0
8428
marktang
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...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
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...
0
5649
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();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
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.