Xml Serialization Helper : Xml Serialization « XML « C# / C Sharp






Xml Serialization Helper

  
using System;
using System.IO;
using System.Xml.Serialization;

namespace Objectware.Silverlight.Testing.Runner.Utilities
{
  /// <summary>
  /// Summary description for SerializationHelper.
  /// </summary>
  public static class SerializationHelper
  {
    private static string InternalSerializer(Type type, object content)
    {
      StringWriter writer = new StringWriter();
      XmlSerializer serializer = new XmlSerializer(type);
      serializer.Serialize(writer, content);
      return writer.ToString();
    }


    public static string SerializeToString(Type type, object content)
    {
      return InternalSerializer(type, content);
    }

    public static string SerializeToString<T>(T content)
    {
      return InternalSerializer(typeof(T), content);
    }


    public static object DeserializeFromString(Type type, string content)
    {
      using (StringReader reader = new StringReader(content))
      {
        XmlSerializer serializer = new XmlSerializer(type);
        return serializer.Deserialize(reader);
      }
    }

    public static T DeserializeFromString<T>(string content)
    {
      using (StringReader reader = new StringReader(content))
      {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(reader);
      }
    }

  }
}

   
    
  








Related examples in the same category

1.Serialize List of Objects
2.Use XmlSerializer
3.Set Xml Attribute when serilzation
4.Use XML Serialization with Custom Objects
5.XmlRootAttribute
6.Takes an XML file and exports the Object it holds
7.Converts an XML string to an object
8.Deserializes/Serializes an xml document back into an object
9.Deserialize with XmlSerializer
10.XmlSerializer Util
11.Serilize to Xml
12.Xml Serialization Helper 2
13.Tiny Xml Serializer
14.Xml Serialization Manager