tags:

views:

78

answers:

6

i have the following two class which convert object into xml string

should i do something like

class Person
{

   public string GetXml()
   {
       //return a xml string
   }  
}

or it is better to create another class which accept the person as a parameter and convert it into XML something like

class PersonSerializer 
{         
     public string Serialize(Person person)
     {
           // return a xml string
     }

 }

Thanks

+2  A: 

Generally, the Serialize method should be on the class you want to serialize; that way, it can access all the private member variables that other classes can't access, and generally do things more efficiently

thecoop
A: 

2nd option. You can create a generic serializer that can serialize different object/classes. Keep your classes as simple only, do what it should do. Serializing is not what a Person should do.

PoweRoy
+3  A: 

The question to ask: What does a person know about XML?

The answer: nothing

So, third vote for a separate serializer.

TomTom
yes i agree but i have seen something similar on .net classes itselfExample: int int1 = 1; int1.ToString();
CliffC
I don't think making a separate class would be a good idea. He is using it to only serialize Person object. So, I think, the object itself should know how to serialize itself.
cornerback84
ToString is seen as a low level item, it comes from system.object and is also the default string representation in the debugger. Compromise. @comerback84 - good answer. Sadly... still thematically wrong. What about parsing?;)
TomTom
thecoop
A: 

To be complete, you should use the IXmlSerializable interface. I.e:

class Person : IXmlSerializable
{
    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        // Provide Schema
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        // Read XML into Object
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        // Write XML here
    }

    #endregion

    // Added as example to what I have said below
    public override string ToString()
    {
        // Make XML String
        return "XML STRING";
    }
}

In response to your comment to TomTom:

yes i agree but i have seen something similar on .net classes itself Example: int int1 = 1; int1.ToString();

What you have seen here is overriding of the ToString() method. I updated the code above to illustrate its usage.

Kyle Rozendo
+1  A: 

There is already a mechanism in .NET for XML serialisation of objects, have a look at this article for details on the attributes you can use to declaratively mark the aspects of your class you want to be serialised.

Your original question is actually asking whether to embed serialisation information into the class to which it pertains or to place it into a separate, though associated class. The advantage of the first approach is that the serialisation code is able to access private members directly and is tightly coupled with the class. The disadvantage is the serialisation code clouds the actual logic of the class — this becomes more apparent if you add binary serialisation too.

There is a actually a mechanism, called serialization surrogates, in .NET for separating serialisation logic out to a separate class. See part 3 of this article for details.

Paul Ruane
A: 

The .NET XmlSerializer can serialize any type. The serialized elements are all pulic read/write properties. Thus, the PersonXmlSerializer already exists ;-)

Seb
the build in .Net XmlSerializer is useful however there is some limitation on it for example it will return an error if there is any interface on any of the class you want to serialize
CliffC
@CliffC: Really...? Would you mind giving me an example ?
Seb
link to issue http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx
CliffC