Simon 的个人资料Simons XNA Game Page照片日志列表更多 工具 帮助

日志


4月19日

XML Serialization

I player around with the XML serialization in C# today and discovered something great.  It's extremely easy to load an XML into a structure and then use the loaded values later on in the program.  I am including the example below.

First we define our Class that will hold all the specific game values.


   public class Settings
   {
  #region Map Specific Values
  public String MapName;
  public int MapSize;
         #endregion

  #region Player Information
  public struct PlayerInfo
  {
     public int X;
     public int Y;

     public PlayerInfo(int x, int y)
     {
      this.X = x;
      this.Y = y;
     }
  }

  //Some default values
  public PlayerInfo[] Players = new PlayerInfo[]
  {
     new PlayerInfo(0,0),
     new PlayerInfo(100,100),
  };
  #endregion

  /// <summary>
  /// Loads the Settings structure with the values from XML.
  /// </summary>
  /// <param name="filename">Path to the XML file.</param>
  /// <returns></returns>
  public static Settings Load(String filename)
  {
     Stream stream = File.OpenRead(filename);
     XmlSerializer serializer = new XmlSerializer(typeof(Settings));
     return (Settings)serializer.Deserialize(stream);
  }
   }

This Settings Class must mirror the structure in the XML file.  A static method is provided to load the XML file into the class object.  Below is the XML file used.  It is quite easy to see the correlation between the class and the XML.  Notice that the ROOT node of the XML document has to match the name of the Class to hold the values.  Each Element in the XML also corresponds to either a structure or a field (it's fairly easy to see this)

<?xml version="1.0" encoding="utf-8" ?>
<Settings>
  <MapName>Map 1</MapName>
  <MapSize>1024</MapSize>
  <Players>
     <PlayerInfo>
        <X>50</X>
        <Y>50</Y>
     </PlayerInfo>
     <PlayerInfo>
       <X>150</X>
       <Y>150</Y>
     </PlayerInfo>
     <PlayerInfo>
        <X>250</X>
        <Y>250</Y>
     </PlayerInfo>
  </Players>
</Settings>


As you can see it's quite easy to see the similarities in the structures.  In order to use this XML file and load it into the Settings class, we simply pass in an instantiated Settings Class object and the path to the XML file.  We are then free to print or do whatever we want with the loaded values.  Below is the example I used to print out the values read from the file.

   public class Reader
   {
  public Reader()
  {
  }

  public void Read(String filename)
  {

     Settings settings = Settings.Load(filename);

     Console.WriteLine("Map Name: " + settings.MapName);
     Console.WriteLine("Map Size: " + settings.MapSize);

     foreach (Settings.PlayerInfo playerinfo in settings.Players)
     {
        Console.WriteLine("Player Info X: " + playerinfo.X + " Y: " + playerinfo.Y);
     }

     Console.ReadKey();

  }
   }


Should be useful if you want to modify your game without recompiling!

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://signot.spaces.live.com/blog/cns!A50EAA47F45992D0!234.trak
引用此项的网络日志