all groups > dotnet clr > may 2007 >
You're in the

dotnet clr

group:

serialization


serialization AVL
5/4/2007 7:02:03 AM
dotnet clr:
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...
Re: serialization Alex Meleta
5/4/2007 11:33:59 PM

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



[quoted text, click to view]


Re: serialization Alex Meleta
5/5/2007 12:00:00 AM

By using the WriteXml() you control writing the object in serialized form.

Use only this:

void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteStartElement("CustomTest");
foreach (string s in text.Split(','))
{
writer.WriteElementString("CustomTestInnerValue", s);
}
writer.WriteEndElement();
}

to provide only Test prop as CustomTest without Test1 and any other:
-
<?xml version="1.0" encoding="utf-8"?>
<Task>
<CustomTest>
<CustomTestInnerValue>vijaya</CustomTestInnerValue>
<CustomTestInnerValue>wajid</CustomTestInnerValue>
<CustomTestInnerValue>vani</CustomTestInnerValue>
</CustomTest>
</Task>
-
and use ReadXml(XmlReader reader) to read this custom xml to matching variables.

Alex
http://devkids.blogspot.com



[quoted text, click to view]

Re: serialization AVL
5/5/2007 1:52:02 AM
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...

[quoted text, click to view]
AddThis Social Bookmark Button