473,789 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2146
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.co m

"Arjen" <bo*****@hotmai l.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.c om> schreef
in bericht news:Oq******** ******@TK2MSFTN GP11.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.co m

"Arjen" <bo*****@hotmai l.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.c om> schreef
in bericht news:Oq******** ******@TK2MSFTN GP11.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.co m

"Arjen" <bo*****@hotmai l.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.co m

"Arjen" <bo*****@hotmai l.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.c om> schreef
in bericht news:Oq******** ******@TK2MSFTN GP11.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.co m

"Arjen" <bo*****@hotmai l.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.c om> schreef
in bericht news:Oi******** ******@TK2MSFTN GP12.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.co m

"Arjen" <bo*****@hotmai l.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.c om> schre ef
in bericht news:Oq******** ******@TK2MSFTN GP11.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.co m

"Arjen" <bo*****@hotmai l.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
4254
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 function:
2
2804
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? It seems to me that there is some overhead associated with calling unserialize().
5
13295
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 (ala StringBuilder) I get utf-16 and instead of the parenthesis (") I get a slash and then a " (\") which makes sense when looking at a character in memory, but not in a string Here is my code XmlSerializer serializer = new XmlSerializer...
14
14318
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
216
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 code complete? (loading and saving) - I have made some starting points. - The // comment tags says where to do "something". - What the "something" means I don't know... but I have tried...
2
2141
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 session. Should I first serialize it to put it in session? I read that this is not necessary, but I'm wondering about memory usage and performance. I realize that serializing/deserializing the object will take time, but will the serialized object...
2
35812
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 the saved settings Can anyone see something that I have missed ?
6
1580
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 is a c# windows aplication. The web service will connect with a access database.
3
3335
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 complete the serialization without incurring the extra bytes. I know there is a simple answer.... Thanks,
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
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
6761
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
5415
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2906
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.