RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Xml + C#?

Xml + C#?

Any good books or ebooks, sites, tutorials etc? I'd like to learn the following:
How to save(write) each variable type, how load(read) each variable type.
 

Jeff

Lord
In RunUO: GDK i save and load tons of crap to xml.

Heres an example of how I save Gumps

Class Definitions:
Gump = Entire Gump
BaseGump = Components of a Gump

Saving:
Gump.cs
Code:
        public void Save(XmlWriter writer)
        {
            OnSaveStarted(this, EventArgs.Empty);

            writer.WriteStartElement("Gump");
            writer.WriteAttributeString("version", GumpVersion.ToString());
            writer.WriteAttributeString("resizable", resizable.ToString());
            writer.WriteAttributeString("dragable", dragable.ToString());
            writer.WriteAttributeString("closable", closable.ToString());
            writer.WriteAttributeString("x", x.ToString());
            writer.WriteAttributeString("y", y.ToString());
            writer.WriteAttributeString("name", name);

            foreach (BaseGump bg in Items)
            {
                writer.WriteStartElement("BaseGump");
                bg.Serialize(writer);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.Close();

            OnSaveCompleted(this, EventArgs.Empty);
        }

BaseGump.cs
Code:
        public virtual void Serialize(XmlWriter writer)
        {
            writer.WriteAttributeString("type", GetType().ToString());
            writer.WriteAttributeString("hue", hue.Index.ToString());
            writer.WriteAttributeString("index", index.ToString());
            writer.WriteAttributeString("z", z.ToString());
            writer.WriteAttributeString("x", location.X.ToString());
            writer.WriteAttributeString("y", location.Y.ToString());
            writer.WriteAttributeString("width", Width.ToString());
            writer.WriteAttributeString("height", Height.ToString());
        }

Loading:
Gump.cs
Code:
        public void Load(XmlDocument doc)
        {
            OnLoadStarted(this, EventArgs.Empty);

            XmlElement root = doc.DocumentElement;// ("Gump");
            XmlNodeList baseGumps = root.GetElementsByTagName("BaseGump");

            for (int i = baseGumps.Count - 1; i >= 0; i--)
            {
                Type type = Type.GetType(baseGumps[i].Attributes["type"].Value);
                BaseGump gump = (BaseGump)Activator.CreateInstance(type);

                string xml = baseGumps[i].OuterXml;
                char[] chars = xml.ToCharArray();
                byte[] xmlData = Utility.ConvertCharToByteArray(chars);
                XmlReader reader = XmlReader.Create(new MemoryStream(xmlData, 0, xmlData.Length));
                reader.Read();
                gump.Deserialize(reader);
                reader.Close();

                Items.Add(gump);
            }

            OnLoadCompleted(this, EventArgs.Empty);
        }

BaseGump.cs
Code:
        public virtual void Deserialize(XmlReader reader)
        {
            hue =  Hues.GetHue(XmlConvert.ToInt32(reader.GetAttribute("hue")));
            index = XmlConvert.ToInt32(reader.GetAttribute("index"));
            z = XmlConvert.ToInt32(reader.GetAttribute("z"));
            location = new Point(XmlConvert.ToInt32(reader.GetAttribute("x")), XmlConvert.ToInt32(reader.GetAttribute("y")));
            size = new Size(XmlConvert.ToInt32(reader.GetAttribute("width")), XmlConvert.ToInt32(reader.GetAttribute("height")));
        }
 
I still don't fully understand this, maybe because I never did any real work with gumps and so it doesn't relate to my previous RUO code...but at any rate, would you mind elaborating? I'd really like to learn this.
 

Jeff

Lord
Storm33229;719068 said:
I still don't fully understand this, maybe because I never did any real work with gumps and so it doesn't relate to my previous RUO code...but at any rate, would you mind elaborating? I'd really like to learn this.

Best luck would be give me an example of something you are trying to save and Ill show u how I would save it.
 
Jeff;719538 said:
Best luck would be give me an example of something you are trying to save and Ill show u how I would save it.
I may haven mentioned this in other threads before; I don't remember though...anyways, I'm getting myself into further C# programming and then Game Programming via C#. So, something game related.

So in a class designated for a player: string name; //just an example.
Either way, I need to be able to save/load all variable types.
 
Top