serialization


G

Guest

Hi,
I'm new serialization concepts....I need some hwlp..
I've a class defined as follows

class CustomSerialization1
{
private string s;

public string Test
{
get { return s; }
set { s = value; }
}

private int i;

public int Test1
{
get { return i; }
set { i = value; }
}
CustomSerialization1 obj = new CustomSerialization1();
obj.Test = "vijaya,wajid,vani";
object.Test1 = 3;
}

if want the the output to be sth below after applying serializatiion

<Task>
<Test>
<TestInnner>Vijaya</TestInnner>
<TestInnner>wajid</TestInnner>
<TestInnner>vani</TestInnner>
</Test>
<Test1>3</Test3>
</Task>

Basically I want to control teh way inwhich my Test property will be
displayed during serialization and deserialization....


please help me out..just let me know if this is possible by any means...
either thru XmlSerialization or Custom Serialziation...
 
Ad

Advertisements

A

Alex Meleta

The simple way is to use default behavior of the XmlSerializer by defining
the new method returning something that the XmlSerializer can simply serialize
be itself:

[XmlRoot("Task")]
public class CustomSerialization1 : IXmlSerializable
{ ...

[XmlIgnore()]
public string Test
{
....
}

[XmlArray("Test")]
[XmlArrayItem("TestInnner")]
public string[] TestAsArray
{
get { return _s.Split(','); }
set { _s = string.Join(",", value); }
}

another (and more complicated approach) is to use the IXmlSerializable interface.
By overriding ReadXml and WriteXml you can control the Xml serialization
by yourselves. E.g.

public class CustomSerialization1 : IXmlSerializable
{ .....
public void WriteXml(XmlWriter writer)
{
.....
writer.WriteStartElement("Test");
foreach (string s in _s.Split(','))
{
writer.WriteElementString("TestInnerValue", s);
}
writer.WriteEndElement();
.....
}
..... }

Alex
http://devkids.blogspot.com
 
G

Guest

Thanks Alex....ur response was quite helpful to me

Alex Meleta said:
The simple way is to use default behavior of the XmlSerializer by defining
the new method returning something that the XmlSerializer can simply serialize
be itself:

[XmlRoot("Task")]
public class CustomSerialization1 : IXmlSerializable
{ ...

[XmlIgnore()]
public string Test
{
....
}

[XmlArray("Test")]
[XmlArrayItem("TestInnner")]
public string[] TestAsArray
{
get { return _s.Split(','); }
set { _s = string.Join(",", value); }
}

another (and more complicated approach) is to use the IXmlSerializable interface.
By overriding ReadXml and WriteXml you can control the Xml serialization
by yourselves. E.g.

public class CustomSerialization1 : IXmlSerializable
{ .....
public void WriteXml(XmlWriter writer)
{
.....
writer.WriteStartElement("Test");
foreach (string s in _s.Split(','))
{
writer.WriteElementString("TestInnerValue", s);
}
writer.WriteEndElement();
.....
}
..... }

Alex
http://devkids.blogspot.com


Hi,
I'm new serialization concepts....I need some hwlp..
I've a class defined as follows
class CustomSerialization1
{
private string s;
public string Test
{
get { return s; }
set { s = value; }
}
private int i;

public int Test1
{
get { return i; }
set { i = value; }
}
CustomSerialization1 obj = new CustomSerialization1();
obj.Test = "vijaya,wajid,vani";
object.Test1 = 3;
}
if want the the output to be sth below after applying
serializatiion

<Task>
<Test>
<TestInnner>Vijaya</TestInnner>
<TestInnner>wajid</TestInnner>
<TestInnner>vani</TestInnner>
</Test>
<Test1>3</Test3>
</Task>
Basically I want to control teh way inwhich my Test property will be
displayed during serialization and deserialization....

please help me out..just let me know if this is possible by any
means... either thru XmlSerialization or Custom Serialziation...
 
Ad

Advertisements

G

Guest

Thanks for the info Alex...
But what I've found is if I use WriteXml(), then I've to manually write teh
xml generation for each and every property of my class....
my class has 20 properties and I want to control the output of only 3
properties...
If I use WriteXml method then, I'm supposed to write the output for all 20
properties thru the XmlWriter...
Can I avoid it? Is there any solution for this...
I want to write the controlled ouput for 3-4 properties of my class and rest
of the properties should be taken care of implicitly... is it possible...

Alex Meleta said:
The simple way is to use default behavior of the XmlSerializer by defining
the new method returning something that the XmlSerializer can simply serialize
be itself:

[XmlRoot("Task")]
public class CustomSerialization1 : IXmlSerializable
{ ...

[XmlIgnore()]
public string Test
{
....
}

[XmlArray("Test")]
[XmlArrayItem("TestInnner")]
public string[] TestAsArray
{
get { return _s.Split(','); }
set { _s = string.Join(",", value); }
}

another (and more complicated approach) is to use the IXmlSerializable interface.
By overriding ReadXml and WriteXml you can control the Xml serialization
by yourselves. E.g.

public class CustomSerialization1 : IXmlSerializable
{ .....
public void WriteXml(XmlWriter writer)
{
.....
writer.WriteStartElement("Test");
foreach (string s in _s.Split(','))
{
writer.WriteElementString("TestInnerValue", s);
}
writer.WriteEndElement();
.....
}
..... }

Alex
http://devkids.blogspot.com


Hi,
I'm new serialization concepts....I need some hwlp..
I've a class defined as follows
class CustomSerialization1
{
private string s;
public string Test
{
get { return s; }
set { s = value; }
}
private int i;

public int Test1
{
get { return i; }
set { i = value; }
}
CustomSerialization1 obj = new CustomSerialization1();
obj.Test = "vijaya,wajid,vani";
object.Test1 = 3;
}
if want the the output to be sth below after applying
serializatiion

<Task>
<Test>
<TestInnner>Vijaya</TestInnner>
<TestInnner>wajid</TestInnner>
<TestInnner>vani</TestInnner>
</Test>
<Test1>3</Test3>
</Task>
Basically I want to control teh way inwhich my Test property will be
displayed during serialization and deserialization....

please help me out..just let me know if this is possible by any
means... either thru XmlSerialization or Custom Serialziation...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Serialization 0
Serializing controls using XML Serialization 3
Serializing an application 0
Serializing to xml. 1
Serial Comms 2
Serialization of objects 1
serialize a collection 2
Serialization of Controls 2

Top