欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

02.xml序列化

程序员文章站 2023-12-29 20:38:16
01.C#转成xmlusing System;using System.Collections;using System.Collections.Generic;using System.Xml.Serialization;using UnityEngine;[Serializable]public class Player{ [XmlAttribute] public int Id { get; set; } [XmlAttribute] publi....

02.xml序列化
01.C#转成xml
02.xml序列化

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

[Serializable]
public class Player
{
    [XmlAttribute]
    public int Id { get; set; }

    [XmlAttribute]
    public string Name { get; set; }

    [XmlArray]
    public List<int> List { get; set; }
    
}

 void InitXml()
    {
        Player player=new Player();
        player.Id = 100;
        player.Name = "得到";
        player.List=new List<int>(){1,2,3};
        FileStream stream=new FileStream(Application.dataPath+"/Players",FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
        StreamWriter streamWriter=new StreamWriter(stream,Encoding.UTF8);
        XmlSerializer xml=new XmlSerializer(typeof(Player));
        xml.Serialize(stream,player);
        streamWriter.Close();
        stream.Close();
    }
}

结果

<?xml version="1.0"?>
<Player xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="100" Name="得到">
  <List>
    <int>1</int>
    <int>2</int>
    <int>3</int>
  </List>
</Player>

本文地址:https://blog.csdn.net/weixin_33950757/article/details/107623616

相关标签: 资源加载

上一篇:

下一篇: