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),会出现乱码,需要更改方法。

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