Valhalla Legends Forums Archive | .NET Platform | Serialization

AuthorMessageTime
K
This is some nifty code I like to use for storing Settings and simple data. It makes it easy to save and load settings, as well as fairly simple to edit the generated xml file by hand if you so desire.
I took out error handling to make the code shorter.
[code]
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace IronBot
{
   [Serializable]
   public class Settings
   {
      public string Name;
      public string Password;
      public string Server;
      public int Port;
      public int CharDelay;
      public int LineDelay;
      public string HomeChannel;
      public string[] Plugins;

      /// <summary>
      /// Initialize all default values in the constructor.
      /// </summary>
      public Settings()
      {
         Name = "anonymous";
         Password = "anonymous";
         Server = "useast.battle.net";
         Port = 6112;
         CharDelay = 25;
         LineDelay = 1500;
         HomeChannel = "open tech support";
         Plugins = new string[1];
         Plugins[0] = "BattleNet.dll";
      }

      /// <summary>
      /// Loads a serialized settings file.
      /// </summary>
      /// <param name="file">The xml file to load</param>
      /// <returns>The specified xml file deserialized, or if no file exists, a default Settings class.</returns>
      public static Settings Load(string file)
      {
         FileStream f;
         XmlSerializer x;

         Settings s = new Settings();
         try
         {
            f = new FileStream(file, FileMode.Open, FileAccess.Read);
            x = new XmlSerializer(s.GetType());
            s = Convert.ChangeType(x.Deserialize(f), s.GetType());
            f.Close();
         }
         catch
         {
// ....
         }

         
         return s;
      }

      public void Save(string file)
      {
         FileStream f;
         XmlSerializer x;
         Settings s = this;
         try
         {
            f = new FileStream(file,FileMode.Create, FileAccess.Write);
            x = new XmlSerializer(this.GetType());
            x.Serialize(f, this);
            f.Close();
         }
         catch
         {
// ....
         }

      
         return;
      }
   }
}[/code]

[code]
// Usage:
Settings config = Settings.Load("mySettings.xml");
foreach(string s in config.Plugins)
{
// ....
}
//...
s.Save("mySettings.xml");
[/code]

If you're lazy like me, you can even add some more information so that your Settings object can be bound to the PropertyGrid control: voila! Instant Configuration Tab.
December 1, 2003, 2:50 AM
K
This is what the generated file looks like: (new post for cleanliness)

[code]
<?xml version="1.0"?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Name>Zorkster</Name>
<Password>***********</Password>
<Server>useast.battle.net</Server>
<Port>6112</Port>
<CharDelay>25</CharDelay>
<LineDelay>1500</LineDelay>
<HomeChannel>open tech support</HomeChannel>
<Plugins>
<string>Core.dll</string>
<string>Zork.dll</string>
</Plugins>
</Settings>
[/code]
December 1, 2003, 3:08 AM
DaRk-FeAnOr
[quote]Server = "useast.battle.net";[/quote]
I am making a bot in C# right now, and what I have found is that the server does not seem to work in a string form such as: "useast.battle.net." The System.Ipaddress.Parse (or w/e) only accepts servers in a "63.240.202.139." Have you found a way for C# to accept word servers?
December 1, 2003, 5:00 AM
Skywing
[quote author=DaRk-FeAnOr link=board=37;threadid=3982;start=0#msg32833 date=1070254844]
[quote]Server = "useast.battle.net";[/quote]
I am making a bot in C# right now, and what I have found is that the server does not seem to work in a string form such as: "useast.battle.net." The System.Ipaddress.Parse (or w/e) only accepts servers in a "63.240.202.139." Have you found a way for C# to accept word servers?
[/quote]
You need to resolve the hostname into an IP address. Take a look at the System.Net.Dns class.
December 1, 2003, 5:36 AM
K
I used a TcpClient for the telnet bot that this snippet came from. TcpClient.Connect() is overloaded to accept (string hostname, int port).

Alternatively, if you want to get all the ip addresses of the host, you can use the Dns.Resolve() method.
December 1, 2003, 5:38 AM

Search