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

Is it posible to serialize more than one array?

Hello,

I have an array "aPeople" with people objects which I serialize like this:

XmlSerializer x = new XmlSerializer( typeof(People[]) );
TextWriter writer = new StreamWriter( "data.xml" );
x.Serialize( writer, aPeople );

Now I have also an array "aAnimal" with animal objects.
Is there a way to put this in the same data.xml file?

Thanks!
Arjen
Nov 15 '05 #1
5 2113
Arjen,

Yes, but you have some things to consider.

Because the xml document that is produced already has a root, you will
have to load both documents and then copy the contents to another, new xml
document that has a different root. Also, deserializing will require some
custom code as well, as you will have to account for this new xml structure
that the XmlSerializer will not understand.

Second, if the arrays share references to any of the elements, then when
reconstructing the arrays, you will get a different result than what you
originally had. Basically, in the arrays, you will have a new instance of
the object which has the same properties set as the instance in the other
array.

To get around both of these issues, I would recommend creating another
array of the arrays, and then using the Binary or Soap formatters to
seralize the contents.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news4.tilbu1.nb.home.nl...
Hello,

I have an array "aPeople" with people objects which I serialize like this:

XmlSerializer x = new XmlSerializer( typeof(People[]) );
TextWriter writer = new StreamWriter( "data.xml" );
x.Serialize( writer, aPeople );

Now I have also an array "aAnimal" with animal objects.
Is there a way to put this in the same data.xml file?

Thanks!
Arjen

Nov 15 '05 #2
Thanks!

Arjen

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht news:Oq**************@TK2MSFTNGP11.phx.gbl...
Arjen,

Yes, but you have some things to consider.

Because the xml document that is produced already has a root, you will
have to load both documents and then copy the contents to another, new xml
document that has a different root. Also, deserializing will require some
custom code as well, as you will have to account for this new xml structure that the XmlSerializer will not understand.

Second, if the arrays share references to any of the elements, then when reconstructing the arrays, you will get a different result than what you
originally had. Basically, in the arrays, you will have a new instance of
the object which has the same properties set as the instance in the other
array.

To get around both of these issues, I would recommend creating another
array of the arrays, and then using the Binary or Soap formatters to
seralize the contents.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news4.tilbu1.nb.home.nl...
Hello,

I have an array "aPeople" with people objects which I serialize like this:
XmlSerializer x = new XmlSerializer( typeof(People[]) );
TextWriter writer = new StreamWriter( "data.xml" );
x.Serialize( writer, aPeople );

Now I have also an array "aAnimal" with animal objects.
Is there a way to put this in the same data.xml file?

Thanks!
Arjen


Nov 15 '05 #3
Hmm, I have tried some things...
But I want to use XML... What must I do with typeof(People[])?
Because I have also a typeof(Animal[])...
How can I made an array for all types with using XmlSerializer?

Can you give me some sample code?

Thanks!
Arjen
/// -----------------------------------------------------------------
public class Person {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Person(string Name) {
_name = Name;
}
}

/// -----------------------------------------------------------------

public class Animal {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Animal(string Name) {
_name = Name;
}
}



"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht news:Oq**************@TK2MSFTNGP11.phx.gbl...
Arjen,

Yes, but you have some things to consider.

Because the xml document that is produced already has a root, you will
have to load both documents and then copy the contents to another, new xml
document that has a different root. Also, deserializing will require some
custom code as well, as you will have to account for this new xml structure that the XmlSerializer will not understand.

Second, if the arrays share references to any of the elements, then when reconstructing the arrays, you will get a different result than what you
originally had. Basically, in the arrays, you will have a new instance of
the object which has the same properties set as the instance in the other
array.

To get around both of these issues, I would recommend creating another
array of the arrays, and then using the Binary or Soap formatters to
seralize the contents.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news4.tilbu1.nb.home.nl...
Hello,

I have an array "aPeople" with people objects which I serialize like this:
XmlSerializer x = new XmlSerializer( typeof(People[]) );
TextWriter writer = new StreamWriter( "data.xml" );
x.Serialize( writer, aPeople );

Now I have also an array "aAnimal" with animal objects.
Is there a way to put this in the same data.xml file?

Thanks!
Arjen


Nov 15 '05 #4
Arjen,

I would just have a type that has the arrays, and then serialize that.
For example:

public class Container
{
public People[] PeopleArray;
public Animal[] AnimalArray;
}

Then, create an instance of Container and then set the fields.
Serialize the instance of Container, and it should work. However, when
deserializing, remember that you are serializing the instance of Container.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news3.tilbu1.nb.home.nl...
Hmm, I have tried some things...
But I want to use XML... What must I do with typeof(People[])?
Because I have also a typeof(Animal[])...
How can I made an array for all types with using XmlSerializer?

Can you give me some sample code?

Thanks!
Arjen
/// -----------------------------------------------------------------
public class Person {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Person(string Name) {
_name = Name;
}
}

/// -----------------------------------------------------------------

public class Animal {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Animal(string Name) {
_name = Name;
}
}



"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht news:Oq**************@TK2MSFTNGP11.phx.gbl...
Arjen,

Yes, but you have some things to consider.

Because the xml document that is produced already has a root, you will
have to load both documents and then copy the contents to another, new xml document that has a different root. Also, deserializing will require some custom code as well, as you will have to account for this new xml

structure
that the XmlSerializer will not understand.

Second, if the arrays share references to any of the elements, then

when
reconstructing the arrays, you will get a different result than what you
originally had. Basically, in the arrays, you will have a new instance of the object which has the same properties set as the instance in the other array.

To get around both of these issues, I would recommend creating another array of the arrays, and then using the Binary or Soap formatters to
seralize the contents.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news4.tilbu1.nb.home.nl...
Hello,

I have an array "aPeople" with people objects which I serialize like

this:
XmlSerializer x = new XmlSerializer( typeof(People[]) );
TextWriter writer = new StreamWriter( "data.xml" );
x.Serialize( writer, aPeople );

Now I have also an array "aAnimal" with animal objects.
Is there a way to put this in the same data.xml file?

Thanks!
Arjen



Nov 15 '05 #5
Nicholas Paldino,

Thanks.
As you can see, I have made a test application.
Maybe you can help me?

Thanks,
Arjen

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schreef
in bericht news:Oi**************@TK2MSFTNGP12.phx.gbl...
Arjen,

I would just have a type that has the arrays, and then serialize that.
For example:

public class Container
{
public People[] PeopleArray;
public Animal[] AnimalArray;
}

Then, create an instance of Container and then set the fields.
Serialize the instance of Container, and it should work. However, when
deserializing, remember that you are serializing the instance of Container.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news3.tilbu1.nb.home.nl...
Hmm, I have tried some things...
But I want to use XML... What must I do with typeof(People[])?
Because I have also a typeof(Animal[])...
How can I made an array for all types with using XmlSerializer?

Can you give me some sample code?

Thanks!
Arjen
/// -----------------------------------------------------------------
public class Person {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Person(string Name) {
_name = Name;
}
}

/// -----------------------------------------------------------------

public class Animal {
private string _name;

public string Name {
get {
return _name;
}
set {
_name = value;
}
}

public Animal(string Name) {
_name = Name;
}
}



"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schre ef
in bericht news:Oq**************@TK2MSFTNGP11.phx.gbl...
Arjen,

Yes, but you have some things to consider.

Because the xml document that is produced already has a root, you will have to load both documents and then copy the contents to another, new xml document that has a different root. Also, deserializing will require some custom code as well, as you will have to account for this new xml structure
that the XmlSerializer will not understand.

Second, if the arrays share references to any of the elements, then when
reconstructing the arrays, you will get a different result than what
you originally had. Basically, in the arrays, you will have a new

instance of the object which has the same properties set as the instance in the other array.

To get around both of these issues, I would recommend creating another array of the arrays, and then using the Binary or Soap formatters to
seralize the contents.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arjen" <bo*****@hotmail.com> wrote in message
news:bu**********@news4.tilbu1.nb.home.nl...
> Hello,
>
> I have an array "aPeople" with people objects which I serialize like

this:
>
> XmlSerializer x = new XmlSerializer( typeof(People[]) );
> TextWriter writer = new StreamWriter( "data.xml" );
> x.Serialize( writer, aPeople );
>
> Now I have also an array "aAnimal" with animal objects.
> Is there a way to put this in the same data.xml file?
>
> Thanks!
> Arjen
>
>



Nov 15 '05 #6

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

Similar topics

8
by: John Smith | last post by:
Hi, I am using a custom Session Handler. session_set_save_handler is working well. But i want to read the data direct from the database. My problem: php don't uses the standard serialize...
2
by: Andrew | last post by:
Some have suggested that using serialize() and unserialize is faster than reading/writing an array to disk as a simple text file using $array = file('numbers.txt'); Can anyone justify this? ...
5
by: Brian Reed | last post by:
I have a class that I want to serialize to an XML string. I want the XML to serialize to utf-8 encoding. When I serialize to an XML file, the data looks great. When I try to serialize to a String...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
0
by: Arjen | last post by:
Hello, I have made a little test application and needs some help. I have two classes with hash tables which I want to serialize inside one xml file. My question is if someone can make the...
2
by: nick | last post by:
Is it recommended to serialize a custom object when putting it in session? I have a User object with properties ID, FirstName, LastName, and an array of Permissions which I would like to keep in...
2
by: Joe | last post by:
Hi I have a Generics List in a PropertyGrid I am able to Serialize it to XML but when I try to deserialize back to the class of the PropertyGrid The Constructor doesn't seem to fire to reload...
6
by: Paez | last post by:
Hello there. My teacher asked me to do a job and I don't know how.. This is the scenario: I must create a client/server application. The server application is a c# web server and the client...
3
by: Andrew Robinson | last post by:
I am using the folllowing code to serialize a object to a byte array (byte). It works fine, but my resulting array is size is being rounded up to the next largest 32768 boundary. How can I...
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
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...
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: 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: 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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.