Max
Max

Reputation: 1349

Analog of php serialize in c#

There in C# available any methods like serialize and unserialize in php?

I need it for best way to transfer an array through network.

Upvotes: 0

Views: 2368

Answers (2)

Stephen Byrne
Stephen Byrne

Reputation: 7485

Here is an example of using basic System.Xml.Serialization to serialise and deserialise an instance of a class. Of course you can do a lot more with this like using attributes to control how the serialiser does its work, etc.

And if you want to use JSON format, then you can use JavascriptSerializer which is very similar or better yet use JSON.Net which is much better.

using System.Xml.Linq;
using System.Xml.Serialization;

static void Main()
{
    //create something silly to serialise:
    var thing = new MyThing(){Name="My thing", ID=0};
    thing.SubThings.Add(new MySubThing{ID=1});
    thing.SubThings.Add(new MySubThing{ID=2});


    //serialise to XML using an XDocument as the output 
    //(you can use any stream writer though)
    //first create the serialiser
    var serialiser = new XmlSerializer(typeof(MyThing));

    //then create an XDocument and get an XmlWriter from it
    var output= new XDocument();
    using(var writer = output.CreateWriter())
    {
       serialiser.Serialize(writer,thing);
    }

    Console.WriteLine(output.ToString());

    /*expected output:
     <MyThing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Name>My thing</Name>
      <ID>0</ID>
      <SubThings>
        <MySubThing>
          <ID>1</ID>
        </MySubThing>
        <MySubThing>
          <ID>2</ID>
        </MySubThing>
      </SubThings>
    </MyThing>

    */

    //Send across network
    //...
    //On other end of the connection, deserialise from XML.
    //Again using XDocument as the input
    //or any Stream Reader containing the data
    var source = output.ToString();
    var input = XDocument.Parse(source);
    MyThing newThing = null;
    using (var reader = input.CreateReader())
    {
      newThing = serialiser.Deserialize(reader) as MyThing;
    }
    if (newThing!=null)
    {
       Console.WriteLine("newThing.ID={0}, It has {1} Subthings",newThing.ID,newThing.SubThings.Count);
    }
    else
    {
        //something went wrong with the de-serialisation.
    }
}

//just some silly classes for the sample code.
public class MyThing
{
   public string Name{get;set;}
   public int ID{get;set;}
   public List<MySubThing> SubThings{get;set;}
   public MyThing()
   {
        SubThings=new List<MySubThing>();
   }
}

public class MySubThing
{
  public int ID{get;set;}
}

Hope that gets you pointed in the right direction...

Upvotes: 1

Anatoly Nikolaev
Anatoly Nikolaev

Reputation: 540

MS reccomends that: http://msdn.microsoft.com/en-us/library/vstudio/et91as27.aspx

Also you can use something like this (here is for doubles, but can be modified to any datatype):

Serialize:

 public string Serialize(double[] Source, string Separator)
    {
        string result = "";
        foreach (double d in Source) result += d.ToString()+Separator;
        return result;
    }

Unserialize

public double[] UnSerialize(string Source, char[] separators)
{
    //splitting Source array by separators   "1|2|3" => Split by '|' => [1],[2],[3]
    string[] tmp = Source.Split(separators, StringSplitOptions.RemoveEmptyEntries);
    double[] result = new double[tmp.Length];
    for(int i=0;i<tmp.Length) {result[i]=Convert.ToDouble(tmp[i]);}
    return result;
}

Upvotes: 1

Related Questions