XNA Creators Club Online
Page 1 of 1 (19 items)
Sort Posts: Previous Next

I need a little help writing XML files to save my game.

Last post 6/30/2009 7:49 PM by dadoo Games. 18 replies.
  • 6/23/2009 3:23 PM

    I need a little help writing XML files to save my game.

    I've been looking at the sample available on msdn How To: Serialize Data which looks like the approach I want to take to save information from my game; however, I haven't worked with many of these things before so I'm a bit confused.  My code looks like this:
    1     [Serializable] 
    2     public struct SaveGameData 
    3     { 
    4         public string PlayerName; 
    5         public Vector2 AvatarPosition; 
    6         public int Level; 
    7         public int Score; 
    8     } 
    9  
    10  
    11     public class SaveGame 
    12     { 
    13         public SaveGame(StorageDevice device) 
    14         { 
    15             // Create the data to save. 
    16             SaveGameData data = new SaveGameData(); 
    17             data.PlayerName = "Hiro"
    18             data.AvatarPosition = new Vector2(360, 360); 
    19             data.Level = 11; 
    20             data.Score = 4200; 
    21  
    22             // Open a storage container. 
    23             StorageContainer container = 
    24                 device.OpenContainer("StorageDemo"); 
    25  
    26             // Get the path of the save game. 
    27             string filename = Path.Combine(container.Path, "savegame.sav"); 
    28  
    29             // Open the file, creating it if necessary. 
    30             FileStream stream = File.Open(filename, FileMode.OpenOrCreate); 
    31  
    32             // Convert the object to XML data and put it in the stream. 
    33             XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData)); 
    34             serializer.Serialize(stream, data); 
    35  
    36             // Close the file. 
    37             stream.Close(); 
    38  
    39             // Dispose the container, to commit changes. 
    40             container.Dispose(); 
    41  
    42         } 
    43     } 

    It is basically the same as the msdn example right now, I just want to get it to link into my game and run before I try putting my own information in there but I have a few questions on how to do so....
    1.  This passes in a StorageDevice, I understand what a StorageDevice is but MSDN StorageDevice wasn't very helpful.  What can I pass into this and how?  Right now I don't have a 'StorageDevice', should I create one in my Gameplay Screen or what exactly?
    2.  Am I able to use the XmlSerializer with both the PC and XBox?  Right now I do not have access to a 360 (a bad thing, I know) so I haven't been able to test on the 360 and I remember somebody telling me some type of content reader/writer was available to PC but not XBox but I don't remember what it was.

    Thanks :)
  • 6/23/2009 3:31 PM In reply to

    Re: I need a little help writing XML files to save my game.

    1) you need to use the guide to prompt the user for a StorageDevice using Guide.BeginShowStorageDeviceSelector to get your asynchronous result, and Guide.EndShowStorageDeviceSelector to get the selected device
    2) the XmlSerializer is available on Windows and the Xbox. You are probably thinking of the IntermediateSerializer, which is only available on Windows
  • 6/23/2009 3:46 PM In reply to

    Re: I need a little help writing XML files to save my game.

    You're better off with the following sample http://msdn.microsoft.com/en-us/library/bb203920.aspx 
    Follow the second sample regarding AsyncCallback.

    There are 2 gotchas both noted by Jason Doucette and LotusXP in the comments on that page.
    On Windows you'll find the Guide won't show but still processes correctly.
  • 6/23/2009 4:23 PM In reply to

    Re: I need a little help writing XML files to save my game.

    I appreciate the help guys.  I'm looking at how to integrate this into my game which I've used the NetworkStateManagementSample as a starting point; however, I'm not sure how to put this in.  I want to have two or more types of saves (1 for the game, 1 for options, 1 for unit customization probably) so I am trying to figure out when I need to get the StorageDevice.  Should it be when I'm about to attempt to save?  The sample from Maia seems like it is in the base of the game which I guess is a little confusing to me.  It looks like you would put it in whichever screen you are saving from but is that correct?  Thanks.
  • 6/23/2009 6:12 PM In reply to

    Re: I need a little help writing XML files to save my game.

    The first time you try to save get the device and keep it around. Make sure you check every time before trying to save that the device is connected.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 6/24/2009 6:14 PM In reply to

    Re: I need a little help writing XML files to save my game.

    So I've managed to get the sample working without to much trouble; however, it is saving under Document/SavedGames/StorageDemo/Player1.  I want these xml files to be something accessible to my project so that the player can load the game and the unit save files are automatically loaded in.  How can I make my game read this location and process these files?  Thanks for the help guys, this made things so simple when I was completely clueless before hand.
  • 6/24/2009 6:27 PM In reply to

    Re: I need a little help writing XML files to save my game.

    You use similar logic to read files. Once you have a StorageDevice, create a storage container with the same name as the one you saved under. The Path property of that StorageContainer will give you the full path to where you saved your files. Then (since you have the file path) you just read in your files using the standard System.IO functionality.
  • 6/24/2009 9:45 PM In reply to

    Re: I need a little help writing XML files to save my game.

    Thanks for the help, things are going well.  Now I'm looking at how to append nodes to my current xml files and I've looked at the File.AppendAllText and FileMode.Append but neither of those seem to be what I am looking for.  For example, my current XML file looks like this:
    BODY{font:x-small 'Verdana';margin-right:1.5em} .c{cursor:hand} .b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none} .e{margin-left:1em;text-indent:-1em;margin-right:1em} .k{margin-left:1em;text-indent:-1em;margin-right:1em} .t{color:#990000} .xt{color:#990099} .ns{color:red} .dt{color:green} .m{color:blue} .tx{font-weight:bold} .db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier} .di{font:small Courier} .d{color:blue} .pi{color:blue} .cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888} .ci{font:small Courier;color:#888888} PRE{margin:0px;display:inline}
      <?xml version="1.0" ?>
    - <SaveGameDataOver xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    - <Test>
      <PlayerName>Hiro3</PlayerName>
    - <AvatarPosition>
      <X>360</X>
      <Y>360</Y>
      </AvatarPosition>
      <Level>11</Level>
      <Score>4200</Score>
      </Test>
      </SaveGameDataOver>

    and I would like to append another Test node, how can I go about doing this?  My current CS code looks like this:
    1 using System; 
    2 using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
    5 using Microsoft.Xna.Framework; 
    6 using Microsoft.Xna.Framework.Storage; 
    7 using System.Xml.Serialization; 
    8 using System.IO; 
    9  
    10 namespace Tactics 
    11
    12     [Serializable] 
    13     public struct SaveGameData 
    14     { 
    15         public string PlayerName; 
    16         public Vector2 AvatarPosition; 
    17         public int Level; 
    18         public int Score; 
    19     } 
    20  
    21     public struct SaveGameDataOver 
    22     { 
    23         public SaveGameData Test; 
    24     } 
    25  
    26     public class SaveGame 
    27     { 
    28         public SaveGame(StorageDevice device) 
    29         { 
    30             // Create the data to save. 
    31             SaveGameData data = new SaveGameData(); 
    32             data.PlayerName = "Hiro3"
    33             data.AvatarPosition = new Vector2(360, 360); 
    34             data.Level = 11; 
    35             data.Score = 4200; 
    36  
    37             SaveGameDataOver test = new SaveGameDataOver(); 
    38  
    39             test.Test = data; 
    40  
    41             // Open a storage container. 
    42             StorageContainer container = 
    43                 device.OpenContainer("Content"); 
    44  
    45             // Get the path of the save game. 
    46             string filename = Path.Combine(container.Path, "savegame.xml"); 
    47  
    48             // Open the file, creating it if necessary. 
    49             FileStream stream = File.Open(filename, FileMode.OpenOrCreate); 
    50  
    51             // Convert the object to XML data and put it in the stream. 
    52             XmlSerializer serializer = new XmlSerializer(typeof(SaveGameDataOver)); 
    53             serializer.Serialize(stream, test); 
    54             // Close the file. 
    55             stream.Close(); 
    56             
    57             // Dispose the container, to commit changes. 
    58             container.Dispose(); 
    59  
    60         } 
    61     } 
    62
    63  

    And one more question that I can see becoming an issue later.  I'm using a custom ContentReader/Writer to import my xml information into my game so one of my current Xml files looks like this:
    1 <?xml version="1.0" encoding="utf-8" ?> 
    2 <XnaContent> 
    3   <Asset Type="System.Collections.Generic.List[ContentReaders.UnitReader]"
    4     <!--Objects are exactly 500px wide and high by default--> 
    5     <!--Testing making a "maze"--> 
    6     <!--UnitSize is a percentage of max size--> 
    7     <Item> 
    8       <ObjectType>Unit</ObjectType> 
    9       <Texture>Images\\Enemy1</Texture> 
    10       <StartingHex>4 1</StartingHex> 
    11       <UnitSize>1.00</UnitSize> 
    12       <MovePoints>10</MovePoints> 
    13     </Item> 
    14   </Asset> 
    15 </XnaContent> 

    How can I add this Asset Type to the files that I create?  Thanks again guys, I'm sorry for the abundance of questions but I've never done this before (actually I haven't done any real game design before the project) so I've got a lot of problems to work through.

  • 6/25/2009 4:34 PM In reply to

    Re: I need a little help writing XML files to save my game.

    Okay so I've got a start, I'm doing this:
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(stream);

                XmlDocumentFragment docFragment = xmlDoc.CreateDocumentFragment();
    and then I think I want to use   docFragment.AppendChild(); but I don't know how to convert my data into an XmlNode that I can use to append.  Can anyone advise me if this is the correct method and if so, how I can go about doing this, or if there is something I am missing?
  • 6/25/2009 8:16 PM In reply to

    Re: I need a little help writing XML files to save my game.


    I would create a List<SaveGameDataOver> and use that on the XmlSerializer.

     

               List<SaveGameDataOver> players = new List<SaveGameDataOver>();  
                players.Add(new SaveGameDataOver() { Test = new SaveGameData() { PlayerName = "Player1", Level = 1, Score = 100 } });  
                players.Add(new SaveGameDataOver() { Test = new SaveGameData() { PlayerName = "Player2", Level = 1, Score = 200 } });  
                players.Add(new SaveGameDataOver() { Test = new SaveGameData() { PlayerName = "Player3", Level = 1, Score = 300 } });  
     
                // Open the file, creating it if necessary.    
                 FileStream stream = File.Open(filename, FileMode.OpenOrCreate);    
         
                 // Convert the object to XML data and put it in the stream.    
                 XmlSerializer serializer = new XmlSerializer(typeof(List<SaveGameDataOver>));    
                 serializer.Serialize(stream, players);    
                 // Close the file.    
                 stream.Close();   

     

     

  • 6/26/2009 1:11 AM In reply to

    Re: I need a little help writing XML files to save my game.

    Okay, so I feel like I'm getting there (thanks to help almost every step along the way) but I've got a big problem formatting.  Here is my CS:
    1 using System; 
    2 using System.Collections.Generic; 
    3 using System.Linq; 
    4 using System.Text; 
    5 using Microsoft.Xna.Framework; 
    6 using Microsoft.Xna.Framework.Content; 
    7 using Microsoft.Xna.Framework.Storage; 
    8 using System.Xml.Serialization; 
    9 using System.IO; 
    10 using System.Xml; 
    11 using System.Collections; 
    12 using ContentReaders; 
    13 using Microsoft.Xna.Framework.Input; 
    14 using Microsoft.Xna.Framework.Net; 
    15  
    16 namespace Tactics 
    17
    18     [Serializable] 
    19     public struct SaveGameData 
    20     { 
    21         public string PlayerName; 
    22         public Vector2 AvatarPosition; 
    23         public int Level; 
    24         public int Score; 
    25     } 
    26  
    27     public struct Item 
    28     { 
    29         public UnitReader unit; 
    30     } 
    31  
    32  
    33     public class SaveGame 
    34     { 
    35         public SaveGame(StorageDevice device, ContentManager content) 
    36         { 
    37             // Create the data to save. 
    38             List<UnitReader> data = new List<UnitReader>(); 
    39             /*data.PlayerName = "Hiro3";
    40             data.AvatarPosition = new Vector2(360, 360);
    41             data.Level = 11;
    42             data.Score = 4200;
    43
    44             SaveGameDataOver test = new SaveGameDataOver();
    45
    46             test.Test = data;*/ 
    47  
    48             //add all current data to the list. 
    49  
    50             List<UnitReader> unitList = new List<UnitReader>(); 
    51  
    52             unitList = content.Load<List<UnitReader>>("XMLContent\\UnitSample"); 
    53  
    54             foreach (UnitReader Item in unitList) 
    55             { 
    56                 data.Add(Item); 
    57             } 
    58  
    59             // Open a storage container. 
    60             StorageContainer container = 
    61                 device.OpenContainer("Content"); 
    62  
    63             // Get the path of the save game. 
    64             string filename = Path.Combine(container.Path, "savegame.xml"); 
    65  
    66             // Open the file, creating it if necessary. 
    67             FileStream stream = File.Open(filename, FileMode.OpenOrCreate); 
    68  
    69             // Convert the object to XML data and put it in the stream. 
    70             XmlSerializer serializer = new XmlSerializer(typeof(List<UnitReader>)); 
    71             serializer.Serialize(stream, data); 
    72              
    73             // Close the file. 
    74             stream.Close(); 
    75             //XmlDocument xmlDoc = new XmlDocument(); 
    76             //xmlDoc.Load(stream); 
    77              
    78             //XmlDocumentFragment docFragment = xmlDoc.CreateDocumentFragment(); 
    79             //docFragment.AppendChild(new XmlNode(typeof(SaveGameDataOver))); 
    80  
    81  
    82             // Dispose the container, to commit changes. 
    83             container.Dispose(); 
    84  
    85         } 
    86          
    87     } 
    88
    89  

    which results in this xml:
    1   <?xml version="1.0" ?>  
    2 - <ArrayOfUnitReader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    3 - <UnitReader> 
    4   <ObjectType>Unit</ObjectType>  
    5   <Texture>Images\\Enemy1</Texture>  
    6 - <StartingHex> 
    7   <X>4</X>  
    8   <Y>1</Y>  
    9   </StartingHex> 
    10   <UnitSize>1</UnitSize>  
    11   <MovePoints>10</MovePoints>  
    12   <IsActive>false</IsActive>  
    13   </UnitReader> 
    14 - <UnitReader> 
    15   <ObjectType>Unit2</ObjectType>  
    16   <Texture>Images\\Enemy1</Texture>  
    17 - <StartingHex> 
    18   <X>3</X>  
    19   <Y>1</Y>  
    20   </StartingHex> 
    21   <UnitSize>1</UnitSize>  
    22   <MovePoints>5</MovePoints>  
    23   <IsActive>false</IsActive>  
    24   </UnitReader> 
    25   </ArrayOfUnitReader> 

    However, my current reader/writer uses an XML format like this:
    1 <?xml version="1.0" encoding="utf-8" ?> 
    2 <XnaContent> 
    3   <Asset Type="System.Collections.Generic.List[ContentReaders.UnitReader]"
    4     <!--Objects are exactly 500px wide and high by default--> 
    5     <!--Testing making a "maze"--> 
    6     <!--UnitSize is a percentage of max size--> 
    7     <Item> 
    8       <ObjectType>Unit</ObjectType> 
    9       <Texture>Images\\Enemy1</Texture> 
    10       <StartingHex>4 1</StartingHex> 
    11       <UnitSize>1.00</UnitSize> 
    12       <MovePoints>10</MovePoints> 
    13     </Item> 
    14     <Item> 
    15       <ObjectType>Unit2</ObjectType> 
    16       <Texture>Images\\Enemy1</Texture> 
    17       <StartingHex>3 1</StartingHex> 
    18       <UnitSize>1.00</UnitSize> 
    19       <MovePoints>5</MovePoints> 
    20     </Item> 
    21   </Asset> 
    22 </XnaContent> 

    and I'm clueless on how to change my tags to such a format.  I've looked into XmlNodeType and many other things but they don't seem to meld well with my current approach.  I'm sorry if I seem troublesome with this, I just can't seem to get all the kinks out.  As always, the help is appreciated, thanks.
  • 6/26/2009 7:17 PM In reply to

    Re: I need a little help writing XML files to save my game.

    So by creating an Item struct with the same objects that are contained in my UnitReader, I have managed to get my Xml to look closer to what I'm looking for but I still don't know how I would be able to change the rest of my file.  Currently my format looks like this:
    1   <?xml version="1.0" ?>  
    2 - <ArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    3 - <Item> 
    4   <Texture>Images\\Enemy1</Texture>  
    5   <ObjectType>Unit</ObjectType>  
    6 - <StartingHex> 
    7   <X>4</X>  
    8   <Y>1</Y>  
    9   </StartingHex> 
    10   <UnitSize>1</UnitSize>  
    11   <MovePoints>10</MovePoints>  
    12   </Item> 
    13 - <Item> 
    14   <Texture>Images\\Enemy1</Texture>  
    15   <ObjectType>Unit2</ObjectType>  
    16 - <StartingHex> 
    17   <X>3</X>  
    18   <Y>1</Y>  
    19   </StartingHex> 
    20   <UnitSize>1</UnitSize>  
    21   <MovePoints>5</MovePoints>  
    22   </Item> 
    23   </ArrayOfItem> 

    where what I am looking for is this:
    1 <?xml version="1.0" encoding="utf-8" ?> 
    2 <XnaContent> 
    3   <Asset Type="System.Collections.Generic.List[ContentReaders.UnitReader]"
    7     <Item> 
    8       <ObjectType>Unit</ObjectType> 
    9       <Texture>Images\\Enemy1</Texture> 
    10       <StartingHex>4 1</StartingHex> 
    11       <UnitSize>1.00</UnitSize> 
    12       <MovePoints>10</MovePoints> 
    13     </Item> 
    14     <Item> 
    15       <ObjectType>Unit2</ObjectType> 
    16       <Texture>Images\\Enemy1</Texture> 
    17       <StartingHex>3 1</StartingHex> 
    18       <UnitSize>1.00</UnitSize> 
    19       <MovePoints>5</MovePoints> 
    20     </Item> 
    21   </Asset> 
    22 </XnaContent> 


    Now, I've tried to approach this from both directions and I'm not sure which is correct:
    1.  change my format of the first file to what I am currently using for my reader/writer
    2.  change my reader/writer to accept the input from the format I am currently saving in.

    I haven't gotten either to work.  My strength in programming is with the logic but I'm still very weak when it comes to overall knowledge of using built in components.  If anyone could help me out, I would appreciate it, I'm having a great deal of trouble with this.  Thanks
  • 6/28/2009 12:30 PM In reply to

    Re: I need a little help writing XML files to save my game.

    Ok I see what you are doing.  Your trying to use the XMLSerializer on something that should use the ContentTypeWriter/ContentTypeReader.  There's a good sample over here.
  • 6/29/2009 12:38 AM In reply to

    Re: I need a little help writing XML files to save my game.

    Actually, that is what I am (attempting) to do.  Here is an example of my writer/reader

    Writer:
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Graphics; 
    using Microsoft.Xna.Framework.Content.Pipeline; 
    using Microsoft.Xna.Framework.Content.Pipeline.Graphics; 
    using Microsoft.Xna.Framework.Content.Pipeline.Processors; 
    using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler; 
     
    using ContentReaders; 
     
    namespace ContentExtension 
        /// <summary> 
        /// This class will be instantiated by the XNA Framework Content Pipeline 
        /// to write the specified data type into binary .xnb format. 
        /// 
        /// This should be part of a Content Pipeline Extension Library project. 
        /// </summary> 
        [ContentTypeWriter] 
        public class UnitWriter : ContentTypeWriter<UnitReader> 
        { 
            protected override void Write( 
                    ContentWriter output, 
                    UnitReader value) 
            { 
                output.Write(value.ObjectType); 
                output.Write(value.Texture); 
                output.Write(value.StartingHex); 
                output.Write(value.UnitSize); 
                output.Write(value.MovePoints); 
            } 
     
            public override string GetRuntimeReader( 
                    TargetPlatform targetPlatform) 
            { 
                return typeof(UnitContentReader).AssemblyQualifiedName; 
            } 
        } 
     

    Reader:
    1 using System; 
    2 using Microsoft.Xna.Framework; 
    3 using Microsoft.Xna.Framework.Content; 
    4 using Microsoft.Xna.Framework.Graphics; 
    5  
    6  
    7 namespace ContentReaders 
    8
    9     public class UnitReader 
    10     { 
    11         string texture; 
    12         string objectType; 
    13         Vector2 startingHex; 
    14         float unitSize; 
    15         int movePoints; 
    16         private bool isActive = false
    17  
    18         public string ObjectType 
    19         { 
    20             get { return objectType; } 
    21             set { objectType = value; } 
    22         } 
    23  
    24         public string Texture 
    25         { 
    26             get { return texture; } 
    27             set { texture = value; } 
    28         } 
    29  
    30         public Vector2 StartingHex 
    31         { 
    32             get { return startingHex; } 
    33             set { startingHex = value; } 
    34         } 
    35  
    36         public float UnitSize 
    37         { 
    38             get { return unitSize; } 
    39             set { unitSize = value; } 
    40         } 
    41  
    42         public int MovePoints 
    43         { 
    44             get { return movePoints; } 
    45             set { movePoints = value; } 
    46         } 
    47  
    48         [ContentSerializerIgnore] 
    49         public bool IsActive 
    50         { 
    51             get { return isActive; } 
    52             set { isActive = value; } 
    53         } 
    54     } 
    55  
    56     //This reads my XML Files that have an associated type (can be seen at the top of the xml file) 
    57     //Each public value and public set/get function must have an associated XML value. 
    58     public class  UnitContentReader : ContentTypeReader<UnitReader> 
    59     { 
    60         protected override UnitReader Read( 
    61                 ContentReader input, 
    62                 UnitReader existingInstance) 
    63         { 
    64             UnitReader sprite = new UnitReader(); 
    65  
    66             sprite.ObjectType = input.ReadString(); 
    67             sprite.Texture = input.ReadString(); 
    68             sprite.StartingHex = input.ReadVector2(); 
    69             sprite.UnitSize = input.ReadSingle(); 
    70             sprite.MovePoints = input.ReadInt32(); 
    71             return sprite; 
    72  
    73         } 
    74     } 
    75  
    76
    77  

  • 6/30/2009 3:46 AM In reply to

    Re: I need a little help writing XML files to save my game.

    Okay, so I've looked more carefully through the sample on ziggyware and I noticed that the Asset tag and XnaContent tag must remain the same so the only option that leaves me with is changing the way I am attempting to save my data.  Right now I am using the XMLSerializer attempting to save but now I understand that you are saying I should be using the ContentTypeWriter to save so I read over that section meticulously but I still don't see how I should use that to write a new XML file.  I'm probably missing something that should make this relatively easy but I don't know what that is.  Could anyone provide some guidance here?  Thanks :)
  • 6/30/2009 4:14 AM In reply to

    Re: I need a little help writing XML files to save my game.

    Let's back this up a bit. When you say save your data, you need to clarify from where and for what purpose. We need to know where you are doing the saving and where you are doing the loading before we can best determine how you should be reading and writing data.

    1) If saving on Windows and loading on the Xbox using the ContentPipeline - use the IntermediateSerializer to create your XML files, add them as Content Pipeline assets, and import them using the XML Importer.

    2) If saving on Windows and loading on the Xbox using the XmlSerializer, XmlReader, etc - use the same method to create your files as you use to read them (i.e. write the files out using XmlSerialzer and read them in using XmlSerialzer). Add the files as Content pipeline assets with no processor, and have them copied directly through to the Xbox StorageContainer.TitleLocation.

    3) If saving on Xbox and loading on the Xbox not using the ContentPipeline - same as the previous option, use the same technology when reading and writing (i.e XmlSerializer). The only difference is that you obviously won't be adding the files to the Content project to be copied over to the Xbox, as you will be creating them on the Xbox under tyour StorageDevice.Path (not StorageContainer.TitleLocation).

    4) If saving on Xbox and loading on the Xbox using the ContentPipeline - this can't be done, and what I think you are trying to do.

    If you are saving files on Windows from an editor program and want to feed those file into your game Content project as assets you can easily do that. When you process an XML file in the content pipeline and instance of your type gets created from your XML file using the IntermediateSerializer. As such, the XML asset files must be of the format expected by the IntermediateSerializer. You can eaily save an instance of your type out to file in the correct format using the IntermediateSerializer, see here for how to do that. In this way you know your save data will always be of the proper format. The one caution here is that the IntermediateSerializer is Windows only, so if you try to write data out using it on the Xbox it won't work (or likely even compile).

    If you are saving data from your game on the Xbox it doesn't make sense to save it in the format expected by the ContentPipeline anyway. Why? For one, the content pipeline processes XML files when it builds on Windows into XNB files - there is no XML processing performed at runtime when using the content pipeline. Thus, you can't just create a save file on the 360 and the try to load it using a ContentManager. Second, I don't know of way to get your ContentManager to read from anywhere other than the TitleLocation, which is read-only. So even if you could get the content pipeline to build XML files at runtime you couldn't get it to build user-generated files at runtime and load them in the content pipeline due to the path separation.




  • 6/30/2009 4:22 AM In reply to

    Re: I need a little help writing XML files to save my game.

    Right now I am doing all my work in Windows; however, once I get my own 360 I will want to begin testing on that and thus will need to save and load on the Xbox.  Do I need different techniques to do both of these?  I see my problem right now is that I'm using the ContentReader/ContentWriter for the xml files that I wrote to import into my game but then I am trying to use the XmlSerializer to write new Xml files (my save) that I then want to load with the ContentReader.  How should I approach a solution to this problem?  Thanks for the quick response dadoo, I appreciate the help :)
  • 6/30/2009 7:34 PM In reply to

    Re: I need a little help writing XML files to save my game.

    So I took a serious look at the XmlSerializer Class and it has a lot of useful abilities to modify the Xml file such as XmlRootAttribute and such but I can't see a way to meld this with my current ContentReader, especially because the file still seems insistent on outputting <ArrayOfItem> in addition to <Item>, and I still haven't figured out how to add the <XnaContent> tag.  Could someone tell me if I'm looking in the right place to solve this or am I completely off track?  Thanks.
  • 6/30/2009 7:49 PM In reply to

    Re: I need a little help writing XML files to save my game.

    The way I see it, you should do the following:

    • for data created in-game, save the files using XmlSerializer
    • for data being loaded in-game, load using XmlSerialier

    Matching these two actions will ensure that saving and loading user-generated content will work the same across the Xbox and Windows.

    If you want to do the least amount of work, use XmlSerializer to create your default content generated on Windows. When you add them to the ContentPipeline, select No processing and have them simply copied across to the Xbox content folder. Then use your XmlSerializer loading logic from above to load them, not the ContentPipeline.

    If you still want to create data to be loaded in-game using the ContentPipeline (such as a default set of levels, etc and load them using Content.Load<>) you will need to save that data using the IntermediateSerializer. The requirement here is that such content (that will be loaded by the pipeline) needs to be generated on Windows, as it will be built when your game builds.

    Your ContentTypeWriter and ContentTypeReader only apply when loading assets using the COntentPipeline and loading using ContentManager.Load, which you can't do for dynamically created user data. This is why it is better for you to stick with XmlSerializer for what you are trying to do.
Page 1 of 1 (19 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG