Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 10-03-2007, 03:32 PM   #1 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default 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.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 10-03-2007, 03:48 PM   #2 (permalink)
Forum Novice
 
Dereckson's Avatar
 
Join Date: Dec 2005
Location: Belgium
Age: 25
Posts: 230
Send a message via Skype™ to Dereckson
Default

To write and read data in XML and C#, the best way is to design a C# class containing your data and use XmlSerializer to read/write it.

Here a sample from reading and writing a xml preference file:
Koders Code Search - MainOptions.cs - C#

See the save and load methods.

Some very interesting articles about XML and C# can be found on The Code Project - Free Source Code and Tutorials in the XML/XSLT section.
__________________
La connaissance s'accroît quand on la partage.
Share your knowledge, you'll increase it.

Utopia. Votre Monde.
Dereckson is offline   Reply With Quote
Old 10-03-2007, 05:17 PM   #3 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,824
Default

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")));
        }
__________________
Jeff Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 10-16-2007, 03:30 PM   #4 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

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.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Old 10-19-2007, 02:11 AM   #5 (permalink)
ConnectUO Creator
 
Jeff's Avatar
 
Join Date: Jan 2004
Age: 27
Posts: 4,824
Default

Quote:
Originally Posted by Storm33229 View Post
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 Boulanger
ConnectUO - Core Developer

Want to help make ConnectUO better? Click here to submit your ideas/requests
Use your talent to compete against other community members in RunUO hosted coding competitions

If you know XNA (even if its just a little) or are a good artist(2d or 3d) and are interested in making games for a hobby send me a pm or drop by #xna in irc.runuo.com. I'm looking to put together a small game development team.


Please do not pm me for support. If you are having issues please post in the appropriate forum. Thanks for your continued support of both ConnectUO and RunUO
Jeff is offline   Reply With Quote
Old 10-19-2007, 02:50 PM   #6 (permalink)
Forum Expert
 
Join Date: Jul 2003
Location: Arizona
Age: 18
Posts: 3,149
Send a message via AIM to Storm33229 Send a message via Yahoo to Storm33229
Default

Quote:
Originally Posted by Jeff View Post
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.
__________________
Quote:
Originally Posted by Radwen
You should crush your pills and sniff them.
Storm33229 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5