locked
Using xml files with XNA

    Question

  • I'd like to try using a settings file for loading/saving settings in my next XNA game, but I have no idea how to use XML.  Looking at the xml file with SpaceWar, it doesn't look too difficult to learn (ie html easy), but I don't really know how to get started, and I've been unable to find tutorials on the 'net dealing with the issue of beginner xml w/ xna (likely because it's too obvious for most people to think of writing a tutorial for).  Anyway, questions:

    1.  The settings.xml file contains all the settings needed, but the settings.cs file already has these hardcoded.  Why does the .cs file need hardcoded settings if it's just planning to load the .xml file every time the game is starting up ?

    2.  This code:


            public void Save(string filename)
            {
                Stream stream = File.Create(filename);

                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(stream, this);
                stream.Close();
            }

            public static Settings Load(string filename)
            {
                Stream stream = File.OpenRead(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                return (Settings)serializer.Deserialize(stream);
            }

    Is this all that's really needed as functions for loading/saving these settings from/to the xml file ?  If so, that's.....pretty simple. 

    3.  Is creating my own .xml file and using it with my program as simple as just typing in each <variable>variable_here</variable> into the .xml file, and the settings .cs file I load it into will automatically take that data and put it where it's supposed to go, assuming all the variables in the .cs file are set up to hold the .xml data properly ?


    Thanks so much. 
    Friday, March 02, 2007 3:37 AM

Answers

  •  MoP73 wrote:
    I'd like to try using a settings file for loading/saving settings in my next XNA game, but I have no idea how to use XML.  Looking at the xml file with SpaceWar, it doesn't look too difficult to learn (ie html easy),

    Yep, XML is "HTML easy", and all of your comments show you're on the right track.

     

    1.  The settings.xml file contains all the settings needed, but the settings.cs file already has these hardcoded.  Why does the .cs file need hardcoded settings if it's just planning to load the .xml file every time the game is starting up ?

    All it's doing is providing defaults in case the xml file doesn't exist or can't be found.  You could decide that you always want to read from an XML file, but then you'd have to make sure it always existed.

    2.  This code:


            public void Save(string filename)
            {
                Stream stream = File.Create(filename);

                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(stream, this);
                stream.Close();
            }

            public static Settings Load(string filename)
            {
                Stream stream = File.OpenRead(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                return (Settings)serializer.Deserialize(stream);
            }

    Is this all that's really needed as functions for loading/saving these settings from/to the xml file ?  If so, that's.....pretty simple. 

    Yep, that's it.

    3.  Is creating my own .xml file and using it with my program as simple as just typing in each <variable>variable_here</variable> into the .xml file, and the settings .cs file I load it into will automatically take that data and put it where it's supposed to go, assuming all the variables in the .cs file are set up to hold the .xml data properly ?

    The short, simple answer is, "yes, that's all there is to it".  You can get a little fancier with XML if you want to.  But for most simple configuration files (like the one in Spacewar), there's no need to.

    By default, the XmlSerializer assumes all variables are stored as XML Elements, which, as you've noticed, look very much like HTML Elements (<ELEMENT-NAME>element-value</ELEMENT-NAME>).  Like HTML, XML also supports attributes, which look like:

    <ELEMENT-NAME attribute_1="attribute_1-value" attribute_2="attribute_2-value">element-value</ELEMENT-NAME>

    You can tag your class variables to specify whether each should be stored as an Element or an Attribute.  For example, you might want to be able to define the Sprites your game uses by saying:

    <sprite x="50" y="100" width="32" height="32">ship-texture</sprite>

    You could then have a Sprite class (or struct) that defines its x, y, width, and height values, along with the texture name.  It would look something like:

    class Sprite

    {

        [XmlAttribute()]

        public int X;

     

        [XmlAttribute()]

        public int Y;

     

        [XmlAttribute()]

        public int Width;

     

        [XmlAttribute()]

        public int Height;

     

        public string TextureName;

    }

    If you really want to understand all of the details, go here:

    http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

    Friday, March 02, 2007 8:09 AM

All replies

  •  MoP73 wrote:
    I'd like to try using a settings file for loading/saving settings in my next XNA game, but I have no idea how to use XML.  Looking at the xml file with SpaceWar, it doesn't look too difficult to learn (ie html easy),

    Yep, XML is "HTML easy", and all of your comments show you're on the right track.

     

    1.  The settings.xml file contains all the settings needed, but the settings.cs file already has these hardcoded.  Why does the .cs file need hardcoded settings if it's just planning to load the .xml file every time the game is starting up ?

    All it's doing is providing defaults in case the xml file doesn't exist or can't be found.  You could decide that you always want to read from an XML file, but then you'd have to make sure it always existed.

    2.  This code:


            public void Save(string filename)
            {
                Stream stream = File.Create(filename);

                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                serializer.Serialize(stream, this);
                stream.Close();
            }

            public static Settings Load(string filename)
            {
                Stream stream = File.OpenRead(filename);
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                return (Settings)serializer.Deserialize(stream);
            }

    Is this all that's really needed as functions for loading/saving these settings from/to the xml file ?  If so, that's.....pretty simple. 

    Yep, that's it.

    3.  Is creating my own .xml file and using it with my program as simple as just typing in each <variable>variable_here</variable> into the .xml file, and the settings .cs file I load it into will automatically take that data and put it where it's supposed to go, assuming all the variables in the .cs file are set up to hold the .xml data properly ?

    The short, simple answer is, "yes, that's all there is to it".  You can get a little fancier with XML if you want to.  But for most simple configuration files (like the one in Spacewar), there's no need to.

    By default, the XmlSerializer assumes all variables are stored as XML Elements, which, as you've noticed, look very much like HTML Elements (<ELEMENT-NAME>element-value</ELEMENT-NAME>).  Like HTML, XML also supports attributes, which look like:

    <ELEMENT-NAME attribute_1="attribute_1-value" attribute_2="attribute_2-value">element-value</ELEMENT-NAME>

    You can tag your class variables to specify whether each should be stored as an Element or an Attribute.  For example, you might want to be able to define the Sprites your game uses by saying:

    <sprite x="50" y="100" width="32" height="32">ship-texture</sprite>

    You could then have a Sprite class (or struct) that defines its x, y, width, and height values, along with the texture name.  It would look something like:

    class Sprite

    {

        [XmlAttribute()]

        public int X;

     

        [XmlAttribute()]

        public int Y;

     

        [XmlAttribute()]

        public int Width;

     

        [XmlAttribute()]

        public int Height;

     

        public string TextureName;

    }

    If you really want to understand all of the details, go here:

    http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

    Friday, March 02, 2007 8:09 AM
  • Thanks Thomas, your reply helped a lot. 
    Friday, March 02, 2007 2:59 PM