473,387 Members | 1,771 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,387 software developers and data experts.

XML: Deserializing Objects

I'm having difficulties deserializing some objects.
Consider the following example of what I'm trying to do:
----------------------------------------------------------
class Person
{
string name;
}

class Info
{
string A;
string B;
}
----------------------------------------------------------
Don't mind the ommitted XML tags or serialize functions.
The resultant XML file is a combination of a serialized
Info object and a serialized array of Person objects,
like so:
----------------------------------------------------------
<xml ...?>
<global>
<Info ...>
<stringA>blah</stringA>
<stringB>blah</stringB>
</info>
<ArrayOfPerson ...>
<Person>
<name>bob</name>
</Person>
Jul 19 '05 #1
1 2024
The solution is to use a new XMLSerializer. Apparently
you can't use the same serializer object for two
different object types.

-----Original Message-----
I'm having difficulties deserializing some objects.
Consider the following example of what I'm trying to do:
--------------------------------------------------------- -class Person
{
string name;
}

class Info
{
string A;
string B;
}
--------------------------------------------------------- -Don't mind the ommitted XML tags or serialize functions.The resultant XML file is a combination of a serialized
Info object and a serialized array of Person objects,
like so:
--------------------------------------------------------- -<xml ...?>
<global>
<Info ...>
<stringA>blah</stringA>
<stringB>blah</stringB>
</info>
<ArrayOfPerson ...>
<Person>
<name>bob</name>
</Person>
.
.
.
</ArrayOfPerson>
</global>
--------------------------------------------------------- -Now, when I want to deserialze, here's the method I've
been trying (error-checking and some declarations
ommitted):

fs = new FileStream
(openFileDialog.FileName,FileMode.Open);
XmlTextReader xmlr = new XmlTextReader(fs);

// Move to Info object in XML file
xmlr.ReadStartElement();
xmlr.ReadInnerXml();

XmlSerializer xmls = new XmlSerializer(typeof(Info));
Info myInfo = (Info)xmls.Deserialize(xmlr);
// Move to array of People objects
xmlr.ReadStartElement();

// Method 1
//ArrayList list = new ArrayList();
//while (xmlr.Read())
//{
// list.Add((Person)xmls.Deserialize(xmlr));
//}
//Person[] ppl = (Person[])list.ToArray(typeof(Person));

// Method2
xmlr.Read();
Person[] ppl = (Person[])xmls.Deserialize(xmlr);
--------------------------------------------------------- -
I've tried both method 1 and method, and in both cases I
get an exception: "System.InvalidOperationException:
<Person xmlns="> was not expected..." This exception of
course, occurs when I try to get the xmlSerializer objectto deserialize the array of Person objects. The first
object is deserialized just fine.

So, I need some serious help! Is my approach wrong? Am
I missing something? Any ideas?

Thanks!
.

Jul 19 '05 #2

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

Similar topics

1
by: Justin Armstrong | last post by:
I'm having difficulties deserializing some objects. Consider the following example of what I'm trying to do: ---------------------------------------------------------- class Person { string...
3
by: Chrigel | last post by:
Hi all, We have problems deseralizing objects previously serialized as XML. This did work fine with .NET 1.1 but since we have installed SP1, deserializing fails (but serializing works). The...
4
by: Jeff T. | last post by:
Hello, I have an existing set of C# classes that encapsulate our application data. They are in a heirachy with each subclass defining more specific types of data. I would like to serialize these...
0
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the...
5
by: Brad | last post by:
I would like to serialize an arraylist of objects to xml so I can store the xml in a database column. How would I code the serializing and deserializing? Below is a (overly) simple, incomplete...
4
by: Chris | last post by:
Hi all, I can serialize and deserialize a single class, but I'm wondering if it's possible to serialize/deserialize different class types in the same file? For example, I have a base class, and...
3
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary...
0
by: Sivajee Akula | last post by:
Hello All, I am trying to consume a .NET Service from Adobe LiveCycle Workflow. The service deals with complex objects. I am getting the following exception at the time of invocation of the...
1
by: sankaralingam | last post by:
I have a xml file created from Excel. I need to transfer the content to a collection of objects in VB.net. I am new to VB.net and XML i couldn't proceed further. I am getting a exception "There is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...

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.