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..
[quoted text clipped - 34 lines]
> please help me out..just let me know if this is possible by any
> means... either thru XmlSerialization or Custom Serialziation...
AVL - 05 May 2007 09:52 GMT
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...
> The simple way is to use default behavior of the XmlSerializer by defining
> the new method returning something that the XmlSerializer can simply serialize
[quoted text clipped - 78 lines]
> > please help me out..just let me know if this is possible by any
> > means... either thru XmlSerialization or Custom Serialziation...
Alex Meleta - 05 May 2007 10:52 GMT
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
> Thanks for the info Alex...
> But what I've found is if I use WriteXml(), then I've to manually
[quoted text clipped - 10 lines]
> of the properties should be taken care of implicitly... is it
> possible...