XML Unity 中的序列化 - 数组是否包含不同的数组项?
XML Serialization in Unity - Have an array hold different arrayitems?
所以我在 Unity 中开发我的游戏,我遇到了一个关于 XML 的问题。我已经建立了一个系统,这要归功于一个教程,该教程允许我通过从 XML 数据库读取数据来创建项目。但是有一个问题。我想将我的 XML 文件设置为按如下方式读取:
<resistance>
<physical>
<phy>60.0</phy>
</physical>
<air>
<air>50.0</air>
</air>
</resistance>
不过,我还没有找到设置为根签入数据的方法。
XML文件格式如下:
<Item>
<id>0</id>
<name>Helmet</name>
<resistance>
<physical>
<phy>60.0</phy>
</physical>
<air>
<air>50.5</air>
</air>
</resistance>
</Item>
[XmlArray("resistance"), XmlArrayItem("physical")] 只读取部分。我也试过按如下方式编写所有内容:
[XmlArray("resistance"), XmlArrayItem("physical"), XmlArrayItem("phy")]
public float[] phyres;
[XmlArray("air"), XmlArrayItem("air")]
public float[] airres;
但是XML文件就变得乱七八糟了,虽然读取了数据,得到了正确的电阻,后面的却没有读取,好像电阻变成了[=35=的新的永久根] 文件。
提前谢谢你。
编辑:换句话说,我想在我的 child 中有一个子根,并在那里保存几个不同的数组。
编辑:编辑:谢谢jdweng,写起来更简单了:
[XmlElement("resistance"), XmlArrayItem("physical")]
public float[] phyres;
[XmlElement("air")]
public float[] airres;
但我仍然遇到同样的问题。 root/namespace 设置为 ,然后从该名称空间读取所有内容。连后期都不影响。
据我了解,您的要求是包含一个 physical
和一个 air
元素的 resistance
元素。这个:
[XmlElement("resistance"), XmlArrayItem("physical")]
public float[] phyres;
[XmlElement("air")]
public float[] airers;
不代表。它意味着包含多个 physical
元素的 resistance
元素 后跟 一个 air
元素。
这是一个 class 结构,它反映了您的 XML:
public class Item
{
[XmlElement("id")]
public int Id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("resistance")]
public Resistance Resistance { get; set; }
}
public class Resistance
{
[XmlArray("physical")]
[XmlArrayItem("phy")]
public float[] Phyres { get; set; }
[XmlArray("air")]
[XmlArrayItem("air")]
public float[] Air { get; set; }
}
所以我在 Unity 中开发我的游戏,我遇到了一个关于 XML 的问题。我已经建立了一个系统,这要归功于一个教程,该教程允许我通过从 XML 数据库读取数据来创建项目。但是有一个问题。我想将我的 XML 文件设置为按如下方式读取:
<resistance>
<physical>
<phy>60.0</phy>
</physical>
<air>
<air>50.0</air>
</air>
</resistance>
不过,我还没有找到设置为根签入数据的方法。
XML文件格式如下:
<Item>
<id>0</id>
<name>Helmet</name>
<resistance>
<physical>
<phy>60.0</phy>
</physical>
<air>
<air>50.5</air>
</air>
</resistance>
</Item>
[XmlArray("resistance"), XmlArrayItem("physical")] 只读取部分。我也试过按如下方式编写所有内容:
[XmlArray("resistance"), XmlArrayItem("physical"), XmlArrayItem("phy")]
public float[] phyres;
[XmlArray("air"), XmlArrayItem("air")]
public float[] airres;
但是XML文件就变得乱七八糟了,虽然读取了数据,得到了正确的电阻,后面的却没有读取,好像电阻变成了[=35=的新的永久根] 文件。 提前谢谢你。
编辑:换句话说,我想在我的 child 中有一个子根,并在那里保存几个不同的数组。
编辑:编辑:谢谢jdweng,写起来更简单了:
[XmlElement("resistance"), XmlArrayItem("physical")]
public float[] phyres;
[XmlElement("air")]
public float[] airres;
但我仍然遇到同样的问题。 root/namespace 设置为 ,然后从该名称空间读取所有内容。连后期都不影响。
据我了解,您的要求是包含一个 physical
和一个 air
元素的 resistance
元素。这个:
[XmlElement("resistance"), XmlArrayItem("physical")]
public float[] phyres;
[XmlElement("air")]
public float[] airers;
不代表。它意味着包含多个 physical
元素的 resistance
元素 后跟 一个 air
元素。
这是一个 class 结构,它反映了您的 XML:
public class Item
{
[XmlElement("id")]
public int Id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("resistance")]
public Resistance Resistance { get; set; }
}
public class Resistance
{
[XmlArray("physical")]
[XmlArrayItem("phy")]
public float[] Phyres { get; set; }
[XmlArray("air")]
[XmlArrayItem("air")]
public float[] Air { get; set; }
}