2

我有序列化 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)我指望你的帮助

4

1 回答 1

1

你有没有帮个忙把事情收拾一下……工作也很好。

代码在这里:

internal class Program
    {
        private static void Main(string[] args)
        {
            ItemBag item = new ItemBag()
            {
                Bag = new Bag
                {
                    BagConfig = new BagConfig { AncientRate = 1, ZenDrop = 1, ExcRate = 1, ItemRate = 1, Name = "ss", SocketRate = 1 }
                },
                Default = new Default
                {
                    DefaultConfig = new DefaultConfig { Addopt = 1, Anc = 1, Category = 1, Exc = 1, ID = 1, Luck = 1, MaxLevel = 1, MinLevel = 1, Skill = 1, Sock = 1 }
                },
                Items = new List<Item>()
                {
                    { new Item { Addopt = 1, Anc = 1, Category = 1, Exc = 1, ID = 1, Luck = 1, MaxLevel = 1, MinLevel = 1, Skill = 1, Sock = 1 }}
                }
            };

            string serialized = item.XmlSerialize();

            Console.WriteLine(serialized);
            Console.ReadLine();
        }
    }

    [XmlRoot]
    public class ItemBag
    {
        public Bag Bag { get; set; }

        public Default Default { get; set; }

        [XmlArray("Items")]
        [XmlArrayItem("Item")]
        public List<Item> Items { get; set; }

        public ItemBag()
        {
            Bag = new Bag();
            Default = new Default();
            Items = new List<Item>();
        }
    }

    public class Bag
    {
        public BagConfig BagConfig { get; set; }

        public Bag()
        {
            BagConfig = new BagConfig();
        }
    }

    public class BagConfig
    {
        [XmlAttribute]
        public string Name { get; set; }

        [XmlAttribute]
        public int ZenDrop { get; set; }

        [XmlAttribute]
        public int ItemRate { get; set; }

        [XmlAttribute]
        public int ExcRate { get; set; }

        [XmlAttribute]
        public int AncientRate { get; set; }

        [XmlAttribute]
        public int SocketRate { get; set; }
    }

    public class Default
    {
        public DefaultConfig DefaultConfig { get; set; }

        public Default()
        {
            DefaultConfig = new DefaultConfig();
        }
    }

    public class DefaultConfig
    {
        [XmlAttribute("cat")]
        public int Category { get; set; }

        [XmlAttribute("id")]
        public int ID { get; set; }

        [XmlAttribute("minlv")]
        public int MinLevel { get; set; }

        [XmlAttribute("maxlv")]
        public int MaxLevel { get; set; }

        [XmlAttribute("skill")]
        public int Skill { get; set; }

        [XmlAttribute("luck")]
        public int Luck { get; set; }

        [XmlAttribute("addopt")]
        public int Addopt { get; set; }

        [XmlAttribute("exc")]
        public int Exc { get; set; }

        [XmlAttribute("anc")]
        public int Anc { get; set; }

        [XmlAttribute("sock")]
        public int Sock { get; set; }
    }

    public class Item
    {
        [XmlAttribute("cat")]
        public int Category { get; set; }

        [XmlAttribute("id")]
        public int ID { get; set; }

        [XmlAttribute("minlv")]
        public int MinLevel { get; set; }

        [XmlAttribute("maxlv")]
        public int MaxLevel { get; set; }

        [XmlAttribute("skill")]
        public int Skill { get; set; }

        [XmlAttribute("luck")]
        public int Luck { get; set; }

        [XmlAttribute("addopt")]
        public int Addopt { get; set; }

        [XmlAttribute("exc")]
        public int Exc { get; set; }

        [XmlAttribute("anc")]
        public int Anc { get; set; }

        [XmlAttribute("sock")]
        public int Sock { get; set; }
    }

你可以使用这个扩展方法:

/// <summary>
        /// Serializes the specified System.Object and returns the serialized XML
        /// </summary>
        /// <typeparam name="T">This item's type</typeparam>
        /// <param name="item">This item</param>
        /// <param name="removeNamespaces">
        ///     <para>Specify whether to remove xml namespaces.</para>para>
        ///     <para>If your object has any XmlInclude attributes, then set this to false</para>
        /// </param>
        /// <returns>Serialized XML for specified System.Object</returns>
        public static string XmlSerialize<T>(this T item, bool removeNamespaces = true)
        {
            object locker = new object();

            XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            xmlns.Add(string.Empty, string.Empty);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;

            lock (locker)
            {
                StringBuilder stringBuilder = new StringBuilder();
                using (StringWriter stringWriter = new StringWriter(stringBuilder))
                {
                    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
                    {
                        if (removeNamespaces)
                        {
                            xmlSerializer.Serialize(xmlWriter, item, xmlns);
                        }
                        else { xmlSerializer.Serialize(xmlWriter, item); }

                        return stringBuilder.ToString();
                    }
                }
            }
        }

将扩展方法放入某个静态类中..

于 2013-03-09T14:04:18.773 回答