Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2007

Tip: Looking for answers? Try searching our database.

serialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AVL - 04 May 2007 15:02 GMT
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...
Alex Meleta - 05 May 2007 00:33 GMT
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...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.