SoFunction
Updated on 2025-03-08

A brief discussion on the conversion of several formats of C# serialization and deserialization

Here are several ways of conversion between serialization and deserialization

First, we introduce how to serialize and serialize object objects, two common ways: string and xml objects;

The first type converts object into a string object, which is relatively simple and has nothing to talk about;

public string ScriptSerialize<T>(T t)
    {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      return (t);
    }

The second way to convert object into an xml object:

public string ScriptSerializeToXML<T>(T t)
    {
      XmlSerializer serializer = new XmlSerializer(typeof(T));
      MemoryStream mem = new MemoryStream();
      XmlTextWriter writer = new XmlTextWriter(mem,Encoding.UTF8);
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      ("","");
      (writer,t,ns);
      ();
      return Encoding.(());
    }

Next, I will mainly talk about deserializing string objects into corresponding objects;

1. Deserialize string objects into object objects

public T ScriptDeserialize<T>(string strJson)
    {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      return <T>(strJson);
    }

2. Deserialize the string object into a list object

public List<T> JSONStringToList<T>(string strJson)
    {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      List<T> objList = <List<T>>(strJson);
      return objList;
    }

3. Deserialize string objects into datatable objects

public DataTable JSONStringToDataTable&lt;T&gt;(string strJson)
    {
      DataTable dt = new DataTable();
      if (("[") &gt; -1)//If it is greater than strJson stores multiple model objects      {
        strJson = ( - 1, 1).Remove(0, 1).Replace("},{", "};{");
      }
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      string[] items = (';');

      foreach (PropertyInfo property in typeof(T).GetProperties())//Get all attributes of type T by reflection      {
        DataColumn col = new DataColumn(,);
        (col);
      }
      //Loop deserialization one by one      for (int i = 0; i &lt; ; i++)
      {
        DataRow dr = ();
        //Deserialize to a T-type object        T temp = &lt;T&gt;(items[i]);
        foreach (PropertyInfo property in typeof(T).GetProperties())
        {
          dr[] = (temp,null);
        }
        (dr);
      }
      return dt;
    }

4. Deserialize the xml object into an object object

public T JSONXMLToObject<T>(string strJson)
    {
      XmlDocument xdoc = new XmlDocument();
      try
      {
        (strJson);
        XmlNodeReader reader = new XmlNodeReader();
        XmlSerializer ser = new XmlSerializer(typeof(T));
        object obj = (reader);
        return (T)obj;
      }
      catch
      {
        return default(T);
      }
    }

Now how to call them with specific instances? It is important to note that the XML object is deserialized to the objcet object

public class LoginObject
  {
     public string Account { get; set;}
     public string Password { get; set;}
   }
LoginObject loginObject = new LoginObject { Account = account, Password = password };
       Serialize = new ();
      //Convert object object to string       string strJson=(loginObject);
     
      //Convert object object to xml object      string strJson = (loginObject);
      

      //Convert to list object       List&lt;LoginObject&gt; list = &lt;LoginObject&gt;(strJson);
      //Convert an xml object to an object object      strJson = (1,  - 1);
      loginObject = &lt;LoginObject&gt;(strJson);
      //Convert string to dataTable      DataTable dt = &lt;LoginObject&gt;(strJson);
      //Convert string to object object      loginObject = &lt;LoginObject&gt;(strJson);

The above article briefly discusses the conversion of several formats of C# serialization and deserialization is all the content I share with you. I hope you can give you a reference and I hope you can support me more.