Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[GUIDE] Parsing XML info on server startup

Newbie Spellweaver
Joined
Oct 5, 2006
Messages
14
Reaction score
0
Hello everybody. That's my first contrib to all people over here. This time, I'll learn how to make your server to read and parse some info at startup step.

I wont provide source codes, so you must use your brain a little :)

1 - Search for the server startup script, just below the last head info, you must put this:

Code:
using System.Xml;
2 - You must find exactly were it begins to load info, so you can use my code in that:

Code:
#region Code Cleanup ASAP

                try
                {
                    XmlTextReader Config = new XmlTextReader(Application.StartupPath + @"\config.xml");

                    while (Config.Read())
                    {
                        switch (Config.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (Config.HasAttributes)
                                {
                                    while (Config.MoveToNextAttribute())
                                    {
                                        if (Config.Name.Equals("dbhost"))
                                            DBHost = Config.Value;
                                        if (Config.Name.Equals("dbuser"))
                                            DBUser = Config.Value;
                                        if (Config.Name.Equals("dbpass"))
                                            DBPass = Config.Value;
                                        if (Config.Name.Equals("dbtable"))
                                            DBTable = Config.Value;
                                        if (Config.Name.Equals("serverip"))
                                            ServerIP = Config.Value;
                                        if (Config.Name.Equals("xprate"))
                                            XPRate = Config.Value;
                                        if (Config.Name.Equals("profxprate"))
                                            ProfXPRate = Config.Value;
                                    }
                                }
                                break;
                        }
                    }
                }
                catch (FileNotFoundException ex)
                {
                    DialogResult result = new DialogResult();
                    result = MessageBox.Show("Config file: " + ex.FileName + " not found. Would you like to enter the params to make a new file?", "Configuration file not found", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        try
                        {
                            XmlTextWriter xmltw = new XmlTextWriter(Application.StartupPath + @"\\config.xml", System.Text.Encoding.UTF8);
                            xmltw.WriteStartDocument();
                            xmltw.WriteStartElement("Hosts");
                            xmltw.WriteStartElement("Host1", null);
                            
                            Console.WriteLine("Provide database ip address:");
                            xmltw.WriteAttributeString("dbhost", Console.ReadLine());
                            
                            Console.WriteLine("Provide database username:");
                            xmltw.WriteAttributeString("dbuser", Console.ReadLine());
                            
                            Console.WriteLine("Provide database password:");
                            xmltw.WriteAttributeString("dbpass", Console.ReadLine());
                            
                            Console.WriteLine("Provide server's table name:");
                            xmltw.WriteAttributeString("dbtable", Console.ReadLine());

                            Console.WriteLine("Enter the IP of server:");
                            xmltw.WriteAttributeString("serverip", Console.ReadLine());

                            Console.WriteLine("XPRate:");
                            xmltw.WriteAttributeString("xprate", Console.ReadLine());

                            Console.WriteLine("ProfXPRate:");
                            xmltw.WriteAttributeString("profxprate", Console.ReadLine());

                            xmltw.WriteEndElement();
                            xmltw.WriteEndElement();

                            xmltw.Close();

                            Console.WriteLine("Restarting server to commit changes...");
                            Console.WriteLine("Press any key to continue...");
                            Console.Read();
                            ServerRestart();
                        }
                        catch (Exception exc)
                        {
                            MessageBox.Show("Error saving server config!\n" + exc.Message.ToString());
                            Environment.Exit(0);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Config file not found, shutting down the server.", "Config file not found", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unexpected error!\n" + ex.Message.ToString());
                    Environment.Exit(0);
                }
                #endregion
And yes, you can use this code to set a lot of other stuff on xml, because they're pretty fast on loading (not as MySQL, but huge better than .INI files).

If you got doubts, just contact me under this topic.

[]'s
 
Newbie Spellweaver
Joined
Nov 13, 2006
Messages
96
Reaction score
0
This will only work in a Windows form application, and not a console, unless you tweak a few things.

Also this wont really make a difference if it's only 5 or so value's that get loaded up once at startup, but good job to help others.
 
Back
Top