2

Ihave problem with serialization XML and dont know how to bite it.. My xml file should look like this:

<?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>

Here is my Serializable class:

[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
        }

Here is Serializable method

    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);
        }

I don't know how to make working an Array of Items (Bag and Default parsing OK) I count for Your help

1

1 Answer 1

1

Did you a favor and tidied things up... working nice too.

Code here:

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; }
    }

And you can use this extension method:

/// <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();
                    }
                }
            }
        }

put the extension method into a static class somewhere..

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You for Cleaning my code... Now i am able to understand what i've done wrong :) . Thanks again for tips and help !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.