0% found this document useful (0 votes)
12 views1 page

Serialization Cs

Uploaded by

devi das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Serialization Cs

Uploaded by

devi das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

26/11/2024 10:26 Serialization.

cs

using System;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;

namespace SharePointStu.LookupField.Common
{
public class Serialization<T> where T : class
{
/// <summary>
/// Serailize object to string
/// </summary>
/// <param name="o">Object to be serialized</param>
/// <returns></returns>
public static string SerializeObject(T o)
{
DataContractSerializer dcs = new DataContractSerializer(o.GetType());

using (MemoryStream ms = new MemoryStream())


{
using (XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(ms))
{
dcs.WriteObject(xdw, o);
xdw.Flush();
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
return xmlDoc.InnerXml;
}
}
}

/// <summary>
/// Deserialize string to object
/// </summary>
/// <param name="sXmlDoc">XML representation of the object </param>
/// <returns></returns>
public static T DeserializeObject(string sXmlDoc)
{
if (string.IsNullOrEmpty(sXmlDoc))
return Activator.CreateInstance(typeof(T)) as T; // return empty object

return Deserialize(sXmlDoc);
}

/// <summary>
/// Deserialize string to object
/// </summary>
/// <param name="sXmlDoc">XML representation of the object </param>
/// <returns></returns>
private static T Deserialize(string sXmlDoc)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sXmlDoc)))
{
object result = dcs.ReadObject(ms);
return result as T;
}
}
}
}

file:///C:/Users/amal.bahrini/Desktop/Data cloud/drive-download-20241126T092541Z-001/Serialization.cs 1/1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy