如何写在Unity在C#中的XML文件? [英] How to write an XML file in C# in Unity?
问题描述
有人请帮助!这是真的困惑了我。我不能在互联网上找到任何人能解释这不够好。因此,这里是我所需要的:
我需要有人来解释如何创建统一的XML文件。人们告诉我,看一个流writer。我搜索了这一点,但没有人给上怎么写的教程。我也不知道什么是.NET的,所以请不要给我,作为一个答案。我已经通过了XML文件的微软页面看起来很好,但我无法找到正确的答案。这是字面上所有我在寻找:
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?我怎样才能使统一读取该文件,并从这样的提取信息?请我在这整个.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.
推荐答案
假设你有一个的播放器的类,它是这样的:
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>
和你猜的播放的是完全一样的,因为我们序列化的原始对象
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屋!