Unity中使用xml文件(序列化)

1、創建Account類。

using System.Collections.Generic;
using System.Xml.Serialization;
using System;

[Serializable]
[XmlRoot("root")]
public class Account
{
    public string username;

    public string password;

    public List<Fan> fans;

    public Account() { }

    public Account(string username, string password, List<Fan> fans)
    {
        this.username = username;
        this.password = password;
        this.fans = fans;
    }
}

2、創建Fan類。

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

[Serializable]
public class Fan
{
    [XmlAttribute]
    public string name;

    [XmlAttribute]
    public int age;

    public Fan() { }
    
    public Fan(string name,int age)
    {
        this.name = name;
        this.age = age;
    }
}

3、創建Test類並掛載在測試場景中的物體上。

using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Test : MonoBehaviour
{
    void Start()
    {
        Fan fan1 = new Fan("張三", 13);
        Fan fan2 = new Fan("李四", 14);
        Fan fan3 = new Fan("吳亦凡", 18);
        List<Fan> fans = new List<Fan>();
        fans.Add(fan1);
        fans.Add(fan2);
        fans.Add(fan3);
        Account account = new Account("蔡徐坤", "jinitaimei", fans);
        using (FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/test.xml",FileMode.Create))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Account));
            xmlSerializer.Serialize(fileStream, account);
        }
    }
}

4、在項目工程中建立StreamingAssets文件夾後,運行測試場景。可發現場景中新建了一個test.xml文件。該文件內容如下。

<?xml version="1.0"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <username>蔡徐坤</username>
  <password>jinitaimei</password>
  <fans>
    <Fan name="張三" age="13" />
    <Fan name="李四" age="14" />
    <Fan name="吳亦凡" age="18" />
  </fans>
</root>

5、上述test類中Start方法中執行的是xml文件序列化的過程。更改test文件內容。進行反序列化操作。

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

public class Test : MonoBehaviour
{
    void Start()
    {
        //Fan fan1 = new Fan("張三", 13);
        //Fan fan2 = new Fan("李四", 14);
        //Fan fan3 = new Fan("吳亦凡", 18);
        //List<Fan> fans = new List<Fan>();
        //fans.Add(fan1);
        //fans.Add(fan2);
        //fans.Add(fan3);
        //Account account = new Account("蔡徐坤", "jinitaimei", fans);
        //using (FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/test.xml",FileMode.Create))
        //{
        //    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Account));
        //    xmlSerializer.Serialize(fileStream, account);
        //}
        Account account;
        using (FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/test.xml", FileMode.Open))
        {
           XmlSerializer xmlSerializer = new XmlSerializer(typeof(Account));
           account = (Account)xmlSerializer.Deserialize(fileStream);
        }
        print(account.username);
        print(account.password);
        foreach (Fan fan in account.fans)
        {
            print("fan:" + fan.name + " " + fan.age);
        }
    }
}

6、運行測試場景。打印臺打印結果如下。反序列化操作成功。

注意:該操作過程使用微軟NET架構的版本是.net4.6,如果是早些版本的化(比如.net2.0),會出現亂碼,需要更改方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章