当前位置: 移动技术网 > 科技>操作系统>windows > 02.xml序列化

02.xml序列化

2020年07月28日  | 移动技术网科技  | 我要评论

在这里插入图片描述
01.C#转成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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网