Is it posible to serialize more than one array?

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

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
 
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.
 
Thanks!

Arjen



Nicholas Paldino said:
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]
- (e-mail address removed)

Arjen said:
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
 
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 said:
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]
- (e-mail address removed)

Arjen said:
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
 
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]
- (e-mail address removed)

Arjen said:
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 said:
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]
- (e-mail address removed)

Arjen said:
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
 
Nicholas Paldino,

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

Thanks,
Arjen





Nicholas Paldino said:
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]
- (e-mail address removed)

Arjen said:
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]" <[email protected]> schre ef
in bericht news:[email protected]...
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]
- (e-mail address removed)

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
 
Back
Top