C#-XML序列化与二进制文件序列化

将一些数据进行序列化,存储成本地文件。

XML正向序列化

序列化的类

FileStream ,以字节流的方式进行文件的读写,通常用于大文件(二进制文件)的读写
StreamReader和StreamWriter,通常用于对文本文件的读写,使用这个的好处时不同的文本文件有不同的编码格式,SteamReader会帮助我们自动处理,StreamWriter也可以指定写入文本的编码方式。

1.序列化的时候,一般要传入流以及类作为参数,若该流为写入流,则会写进文件里。
2.反序列化的时候,一般要传入流作为参数,若该流为读取流,则会读取文件。

[System.Serializable]
public class TestSerialize 
    [XmlAttribute("id")]
    public int m_Id{get;set;}
    [XmlAttribute("Name")]
    public string m_Name { get; set; }
    [XmlElement("List")]
    public List<int> m_List { get; set; }
}

序列化过程:需要创建文件流,创建写入流


public class AssetBundleConfig : MonoBehaviour {
   void Start()
   {
       SerializeTest();
   }
   void SerializeTest()
   {
       TestSerialize testSerialize = new TestSerialize();
       testSerialize.m_Id = 1;
       testSerialize.m_Name = "TTT";
       testSerialize.m_List = new List<int>();
       testSerialize.m_List.Add(2);
       testSerialize.m_List.Add(3);
       XmlSerialize(testSerialize);
   }
   void XmlSerialize(TestSerialize testSerialize)
   {
       //文件流
       FileStream newFileStream = new FileStream(Application.dataPath+"/test.xml",FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
       //写入流
       StreamWriter streamWriter = new StreamWriter(newFileStream, System.Text.Encoding.UTF8);
       //创建一个序列化类
       XmlSerializer xml = new XmlSerializer(testSerialize.GetType());
       //序列化
       xml.Serialize(streamWriter,testSerialize);
       streamWriter.Close();
       newFileStream.Close();
   }
}
XML反向序列化

将XML文件读取成一个类

TestSerialize XmlDeSerialize()
  {
      using (FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
      {
          XmlSerializer xs = new XmlSerializer(typeof(TestSerialize));
          TestSerialize testSerialize = (TestSerialize)xs.Deserialize(fs);
          return testSerialize;
      }
  }
二进制文件正向序列化
 void BinaryTest()
   {
       TestSerialize testSerialize = new TestSerialize();
       testSerialize.m_Id = 1;
       testSerialize.m_Name = "TTT";
       testSerialize.m_List = new List<int>();
       testSerialize.m_List.Add(2);
       testSerialize.m_List.Add(3);
       BinarySerialize(testSerialize);
   }

   void BinarySerialize(TestSerialize serialize)
   {
       using (FileStream fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
       {
           BinaryFormatter bf = new BinaryFormatter();
           bf.Serialize(fs, serialize);
       }
   }
二进制文件反向序列化
void BinaryDeSerializeTest()
  {
      TestSerialize test = BinaryDeSerialize();
      Debug.Log(test.m_Id + " " + test.m_Name);
      for (int i = 0; i < test.m_List.Count; i++)
      {
          Debug.Log(test.m_List[i]);
      }
  }

  TestSerialize BinaryDeSerialize()
  {
      TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes");
      using (MemoryStream memoryStream = new MemoryStream(textAsset.bytes))
      {
          BinaryFormatter binaryFormatter = new BinaryFormatter();
          TestSerialize testSerialize = (TestSerialize)binaryFormatter.Deserialize(memoryStream);
          return testSerialize;
      }
  }

你可能感兴趣的:(#,C#之路)