473,378 Members | 1,411 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,378 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 2021
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.