C# XML中的统一项目数据库
我正在尝试为我的游戏创建一个项目数据库。我希望它包含游戏中的所有物品(武器、消耗品、盔甲等)。但我希望它们都从名为item的父类继承。我看到的所有示例都使用单个项类,没有继承 XML中有没有一种方法可以使我的数据库在反序列化时都是正确的类型?武器类型为武器,盔甲类型为装甲等 我当前的XML以及项目和容器:C# XML中的统一项目数据库,c#,xml,unity3d,C#,Xml,Unity3d,我正在尝试为我的游戏创建一个项目数据库。我希望它包含游戏中的所有物品(武器、消耗品、盔甲等)。但我希望它们都从名为item的父类继承。我看到的所有示例都使用单个项类,没有继承 XML中有没有一种方法可以使我的数据库在反序列化时都是正确的类型?武器类型为武器,盔甲类型为装甲等 我当前的XML以及项目和容器: <?xml version="1.0" encoding="UTF-8"?> <ItemCollection> <Items> <Data
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<DatabaseItem name="Sword">
<Damage>20</Damage>
</DatabaseItem>
<DatabaseItem name="Wand">
<Damage>10</Damage>
</DatabaseItem>
</Items>
</ItemCollection>
20
10
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.Xml.Serialization;
使用System.IO;
[XmlRoot(“itemCollection”)]
公共类ItemContainer
{
[XmlArray(“项”)]
[XmlArrayItem(“数据库项”)]
公共列表项=新列表();
公共静态ItemContainer加载(字符串路径)
{
TextAsset _xml=Resources.Load(路径);
XmlSerializer serializer=新的XmlSerializer(typeof(ItemContainer));
StringReader=新的StringReader(_xml.text);
ItemContainer items=序列化程序。将(读取器)反序列化为ItemContainer;
reader.Close();
退货项目;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.Xml;
使用System.Xml.Serialization;
公共类数据库项
{
[XmlAttribute(“标题”)]
公共字符串标题;
[XmlAttribute(“损害”)]
公众浮标损害;
}
您可以添加一个指定类型的属性。因此,您的XML看起来更像这样:
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<DatabaseItem title="Sword">
<damage>20</damage>
<type>sword</type> <-- New
</DatabaseItem>
<DatabaseItem title="Wand">
<damage>10</damage>
<type>wand</type> <-- New
</DatabaseItem>
</Items>
</ItemCollection>
<?xml version="1.0" encoding="utf-8"?>
<itemCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<DatabaseItem xsi:type="Sword" damage="0" />
<DatabaseItem xsi:type="Axe" damage="0" />
<DatabaseItem xsi:type="Helmet" protection="0" />
<DatabaseItem xsi:type="Shell" protection="0" />
</Items>
</itemCollection>
20
您可以添加一个指定类型的属性。因此,您的XML看起来更像这样:
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<DatabaseItem title="Sword">
<damage>20</damage>
<type>sword</type> <-- New
</DatabaseItem>
<DatabaseItem title="Wand">
<damage>10</damage>
<type>wand</type> <-- New
</DatabaseItem>
</Items>
</ItemCollection>
<?xml version="1.0" encoding="utf-8"?>
<itemCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<DatabaseItem xsi:type="Sword" damage="0" />
<DatabaseItem xsi:type="Axe" damage="0" />
<DatabaseItem xsi:type="Helmet" protection="0" />
<DatabaseItem xsi:type="Shell" protection="0" />
</Items>
</itemCollection>
20
剑不要重新发明轮子。使用序列化程序的标准功能
创建所需的类层次结构:
[XmlInclude(typeof(Sword))]
[XmlInclude(typeof(Axe))]
[XmlInclude(typeof(Helmet))]
[XmlInclude(typeof(Shell))]
public class Item
{
[XmlAttribute("title")]
public string Title;
}
public class Weapon : Item
{
[XmlAttribute("damage")]
public float Damage;
}
public class Armor : Item
{
[XmlAttribute("protection")]
public float Protection;
}
public class Sword : Weapon { }
public class Axe : Weapon { }
public class Helmet : Armor { }
public class Shell : Armor { }
重要!指定所有类都是属性xmlclude
中基类的后代
创建类的多个实例,并将它们添加到容器中:
var sword = new Sword();
var axe = new Axe();
var helmet = new Helmet();
var shell = new Shell();
var container = new ItemContainer();
container.Items.Add(sword);
container.Items.Add(axe);
container.Items.Add(helmet);
container.Items.Add(shell);
现在,序列化之后,xml将如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<DatabaseItem title="Sword">
<damage>20</damage>
<type>sword</type> <-- New
</DatabaseItem>
<DatabaseItem title="Wand">
<damage>10</damage>
<type>wand</type> <-- New
</DatabaseItem>
</Items>
</ItemCollection>
<?xml version="1.0" encoding="utf-8"?>
<itemCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<DatabaseItem xsi:type="Sword" damage="0" />
<DatabaseItem xsi:type="Axe" damage="0" />
<DatabaseItem xsi:type="Helmet" protection="0" />
<DatabaseItem xsi:type="Shell" protection="0" />
</Items>
</itemCollection>
反序列化将正确创建所需类的实例。不要重新发明轮子。使用序列化程序的标准功能
创建所需的类层次结构:
[XmlInclude(typeof(Sword))]
[XmlInclude(typeof(Axe))]
[XmlInclude(typeof(Helmet))]
[XmlInclude(typeof(Shell))]
public class Item
{
[XmlAttribute("title")]
public string Title;
}
public class Weapon : Item
{
[XmlAttribute("damage")]
public float Damage;
}
public class Armor : Item
{
[XmlAttribute("protection")]
public float Protection;
}
public class Sword : Weapon { }
public class Axe : Weapon { }
public class Helmet : Armor { }
public class Shell : Armor { }
重要!指定所有类都是属性xmlclude
中基类的后代
创建类的多个实例,并将它们添加到容器中:
var sword = new Sword();
var axe = new Axe();
var helmet = new Helmet();
var shell = new Shell();
var container = new ItemContainer();
container.Items.Add(sword);
container.Items.Add(axe);
container.Items.Add(helmet);
container.Items.Add(shell);
现在,序列化之后,xml将如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<DatabaseItem title="Sword">
<damage>20</damage>
<type>sword</type> <-- New
</DatabaseItem>
<DatabaseItem title="Wand">
<damage>10</damage>
<type>wand</type> <-- New
</DatabaseItem>
</Items>
</ItemCollection>
<?xml version="1.0" encoding="utf-8"?>
<itemCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<DatabaseItem xsi:type="Sword" damage="0" />
<DatabaseItem xsi:type="Axe" damage="0" />
<DatabaseItem xsi:type="Helmet" protection="0" />
<DatabaseItem xsi:type="Shell" protection="0" />
</Items>
</itemCollection>
反序列化将正确创建所需类的实例。正是我所期待的,正是我所期待的