我有序列化 XML 的问题,不知道如何咬它。我的 xml 文件应该如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ItemBag>
<Bag>
<BagConfig Name="txt" ZenDrop="0" ItemRate = "100" ExcRate="100" AncientRate="0" SocketRate="0"/>
</Bag>
<Default>
<DefaultConfig cat="1" id="1" minlv="0" maxlv="15" skill="1" luck="1" addopt="0" exc= "0" anc="0" sock="0" />
</Default>
<Items>
<Item cat="1" id="1" minlv="0" maxlv="15" skill="1" luck="1" addopt="0" exc="0" anc="0" sock="0" />
<Item cat="7" id="21" minlv="7" maxlv="7" skill="0" luck="0" addopt="0" exc="1" anc="0" sock="0" />
<Item cat="8" id="21" minlv="7" maxlv="7" skill="0" luck="0" addopt="0" exc="1" anc="0" sock="0" />
<Item cat="9" id="21" minlv="7" maxlv="7" skill="0" luck="0" addopt="0" exc="1" anc="0" sock="0" />
</Items>
</ItemBag>
这是我的可序列化类:
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ItemBag
{
public ItemBagBag Bag;
public ItemBagDefault Default;
[System.Xml.Serialization.XmlArrayItemAttribute("Item", IsNullable = true)]
public ItemBagItem[] Items;
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
public partial class ItemBagBag
{
public ItemBagBagBagConfig BagConfig;
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
public partial class ItemBagBagBagConfig
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name;
[System.Xml.Serialization.XmlAttributeAttribute()]
public sbyte ZenDrop;
//.......ETC
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
public partial class ItemBagDefault
{
public ItemBagDefaultDefaultConfig DefaultConfig;
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
public partial class ItemBagDefaultDefaultConfig
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public sbyte cat;
[System.Xml.Serialization.XmlAttributeAttribute()]
public sbyte id;
//........ETC
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute()]
public partial class ItemBagItem
{
[System.Xml.Serialization.XmlAttributeAttribute()]
public sbyte cat;
[System.Xml.Serialization.XmlAttributeAttribute()]
public sbyte id;
//.......ETC
}
这是可序列化的方法
static void SerializationToXml()
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
ItemBag item = new ItemBag()
{
Bag = new ItemBagBag
{
BagConfig = new ItemBagBagBagConfig
{ AncientRate = 1, ZenDrop = 1, ExcRate = 1, ItemRate = 1, Name = "ss", SocketRate = 1 }
},
Default = new ItemBagDefault
{
DefaultConfig = new ItemBagDefaultDefaultConfig
{ addopt = 1, anc = 1, cat = 1, exc = 1, id = 1, luck = 1, maxlv = 1, minlv = 1, skill = 1, sock = 1, Value = "1" }
},
Items = new ItemBagItem[]
{
//doesent work :(
}
};
TextWriter writer = new StreamWriter(@"c:\test.xml");
try
{
XmlSerializer serializer = new XmlSerializer(typeof(ItemBag));
serializer.Serialize(writer, item, namespaces);
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我不知道如何制作一个项目数组(包和默认解析OK)我指望你的帮助