如何在 Unity 中用 C# 编写 XML 文件? [英] How to write an XML file in C# in Unity?

查看:31
本文介绍了如何在 Unity 中用 C# 编写 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请帮忙!这对我来说真的很困惑.我在互联网上找不到任何人可以很好地解释这一点.所以这是我需要的:我需要有人解释如何在 Unity 中创建 XML 文件.人们告诉我看一个流作家.我已经搜索过这个,但没有人给出如何编写它的教程.我也不知道 .NET 是什么,所以请不要给我这个答案.我也浏览了 XML 文件的 microsoft 页面,但我找不到正确的答案.这实际上就是我要找的:

Someone please help! This is really confusing for me. I can't find anyone on the internet that can explain this well enough. So here is what I need: I need someone to explain how to create an XML file in Unity. People have told me to look at a stream writer. I have searched for this but no one has given a tutorial on how to write it. I also do not know what a .NET is so please don't give me that as an answer. I have looked through the microsoft page for XML files as well but I cannot find the right answer. This is literally all I'm looking for:

我希望能够写出这样的东西:

I want to be able to write something like this:

<Player>

    <Level>5<Level>  
    <Health>500<Health>  

<Player>

如何制作这样的文件并将其导入 Unity?我怎样才能让 Unity 读取这个文件并从这样的东西中提取信息?拜托,我是这整个 .NET 和 XML 方面的总n00b.

How can I make a file like this and import it to Unity? How can I make Unity read this file and extract information from something like this? Please i'm a total n00b at this whole .NET and XML thing.

推荐答案

假设您有一个 Player 类,如下所示:

Say you have a Player class that looks like:

[XmlRoot]
public class Player
{
    [XmlElement]
    public int Level { get; set; }

    [XmlElement]
    public int Health { get; set; }
}

这是一个完整的往返行程,可帮助您入门:

Here is a complete round-trip to get you started:

XmlSerializer xmls = new XmlSerializer(typeof(Player));

StringWriter sw = new StringWriter();
xmls.Serialize(sw, new Player { Level = 5, Health = 500 });
string xml = sw.ToString();

Player player = xmls.Deserialize(new StringReader(xml)) as Player;

xml 是:

<?xml version="1.0" encoding="utf-16"?>
<Player xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Level>5</Level>
  <Health>500</Health>
</Player>

你猜player和我们序列化的原始对象完全一样.

And you guess player is exactly the same as the original object we serialized.

如果您想从文件序列化/反序列化,您可以执行以下操作:

If you want to serialize to/deserialize from files you can do something like:

using (var stream = File.OpenWrite("my_player.xml"))
{
    xmls.Serialize(stream, new Player { Level = 5, Health = 500 });
}

Player player = null;
using (var stream = File.OpenRead("my_player.xml"))
{
    player = xmls.Deserialize(stream) as Player;
}

如果您想要显示的 XML:

IF you want exactly the XML you show:

XmlSerializer xmls = new XmlSerializer(typeof(Player));

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = true };
using (var stream = File.OpenWrite("my_player.xml"))
{
    using (var xmlWriter = XmlWriter.Create(stream, settings))
    {
        xmls.Serialize(xmlWriter, new Player { Level = 5, Health = 500 }, ns);
    }
}

Player player = null;
using (var stream = File.OpenRead("my_player.xml"))
{
    player = xmls.Deserialize(stream) as Player;
}

这篇关于如何在 Unity 中用 C# 编写 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆