Jump to content
  • Advertisement
Sign in to follow this  
NDraskovic

Loading XML into XNA 4.0

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey

I made a class that I want to serialize into XML file, and I made a method(s) to do it. This is the class:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
using System.Xml;
namespace Postavke
{

public class PostavkeStanice
{
public bool radnaStanica1_Stanje;
public bool radnaStanica2_Stanje;
public bool radnaStanica3_Stanje;
public bool kraj_Stanje;
MemoryStream ms = new MemoryStream();
public void ClassToXML()
{

XmlTextWriter pisac = new XmlTextWriter(ms, Encoding.UTF8);
pisac.Formatting = Formatting.Indented;
IntermediateSerializer.Serialize(pisac, this, @"C:\Users\NeveN\Documents\Visual Studio 2010\Projects\Diplomski_V_1_0\Diplomski_V_1_0\Diplomski_V_1_0\bin\x86\Debug\Content\Teksture\Postavke.xml");
}

public string XMLToXNA()
{
ms.Seek(0, SeekOrigin.Begin);
TextReader citac = new StreamReader(ms);
return citac.ReadToEnd();
}
}
}



I placed this class into a Game Library, and set the dependencies so that the Game Library compiles before the main project. The entire solution compiles just fine, but there are some problems while running the program. In my Game1 class I created an instance of this class, I called the ClassToXml method in LoadContent method, and XMLToXna in Update method. The ClassToXml works fine, because I can see the .xml file get created in the solution directory. However, when I change the values in the .xml file, those changes do not manifest themselves in the application (by my logic they should because program should call the XMLToXna periodically). The idea is that I change the bool value that determines if an object will be displayed or not.
Can you tell me what I'm doing wrong?

I tried to add the .xml file to Content, but when I do that and try to compile I get the error "There was an error while deserializing intermediate XML. Cannot find type "Postavke.PostavkeStanice" " Edited by NDraskovic

Share this post


Link to post
Share on other sites
Advertisement
I also was struggling very much when i had to serialize data. I don't know much about the subject either and can point you to the blog of Shawn, with his posts about the serializer you should be able to fix your problem.

http://www.shawnhargreaves.com/blogindex.html

However I do not wish to put you on the wrong path but this is how i serialize the data for my map editor.


if (input.KeyPressed(Keys.F1))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(mapData.MapName + ".xml", settings))
{
IntermediateSerializer.Serialize(writer, mapData, null);
}
}


And this is how the helper library looks:

public class MapDataHelper
{
public struct MapData
{
public string MapName { get; set; }
public string TileSet { get; set; }
public int TileWidth { get; set; }
public int TileHeight { get; set; }
public int MapWidth { get; set; }
public int MapHeight { get; set; }
public TileData[] tiles;
}
}


Here is that tiledata class:


public class TileData
{
public int Texture { get; set; }
public bool Walkable { get; set; }
public int Region { get; set; }
}


and you can even derive from that, but i remember it was a pain to get that to show once readed back in.


public class Objects : TileData
{
public int Texture { get; set; }
public bool Walkable { get; set; }
}

Share this post


Link to post
Share on other sites
Hey
I've already read Shawn's blog(s) and it helped me to serialize the data (like I said, when I run my application, it creates the appropriate .xml file). The problem is that he wrote only about serializing, not about deserializing and loading data into the game, during run-time (or at least on start-up). That is the problem I'm having, I need to load the data that will determine how my application works, I've been working on this problem for 3-4 days now and had no progress what so ever.

Share this post


Link to post
Share on other sites
Could you post the XML file?

Since it is asking for Postavke.PostavkeStanice "namespace.class" it seems the problem lies in the serialization of your class.

In your serialization line you saÿ you put "this", but you do not want that whole class to serialize, only these right?

public bool radnaStanica1_Stanje;
public bool radnaStanica2_Stanje;
public bool radnaStanica3_Stanje;
public bool kraj_Stanje;


Why don't you put those in a struct like i did and then put that structs name in the place of "this"?

Share this post


Link to post
Share on other sites
I "solved" this problem, at least the reading part (it was a beginners mistake, I didn't reference my "Postavke" project in my Content project). It works, but I have to rebuild every time when I make some changes to the xml file. I tried to change the .xml "Build Action" from "Compile" to "Content", but when I try to build it, I get an run-time error "The file .. does not exist". I'll try to find some tutorials on Saving and Loading games, I think that is the way to solve this problem, but I don't have time to search it yet. If you have some suggestions please share.
Thanks

Share this post


Link to post
Share on other sites

I "solved" this problem, at least the reading part (it was a beginners mistake, I didn't reference my "Postavke" project in my Content project). It works, but I have to rebuild every time when I make some changes to the xml file. I tried to change the .xml "Build Action" from "Compile" to "Content", but when I try to build it, I get an run-time error "The file .. does not exist". I'll try to find some tutorials on Saving and Loading games, I think that is the way to solve this problem, but I don't have time to search it yet. If you have some suggestions please share.
Thanks

You want the Build Action to be Compile.

http://forums.create.msdn.com/forums/t/27407.aspx

Share this post


Link to post
Share on other sites
Yes, it is set to compile, but now my problem is that I can't change the data during run time, I have to rebuild the whole solution to apply the changes. I've been trying to solve this with "Load game" tutorials, but I'm unsuccessful so far. This is the code I'm using for loading the file:

[ContentSerializerIgnore]
public StorageDevice device;
[ContentSerializerIgnore]
public StorageContainer container;
[ContentSerializerIgnore]
string imeDatoteke = "Postavke.xml";

public void Otvori()
{

IAsyncResult rezultat = device.BeginOpenContainer("Content", null, null);
rezultat.AsyncWaitHandle.WaitOne();

container = device.EndOpenContainer(rezultat);
rezultat.AsyncWaitHandle.Close();
if (!container.FileExists(imeDatoteke))
{
container.Dispose();
return;
}
Stream stream = container.OpenFile(imeDatoteke, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(PostavkeStanice));
PostavkeStanice postavke = (PostavkeStanice)serializer.Deserialize(stream);
stream.Close();
container.Dispose();
}


This method is a part of a class, and when I call it from the main class I get the NullReferenceException error. I placed a breakpoint, and I can see that all of my variables (rezultat,strem,serializer, postavke) have values null. Do you have some idea how to solve this

Share this post


Link to post
Share on other sites

Yes, it is set to compile, but now my problem is that I can't change the data during run time, I have to rebuild the whole solution to apply the changes. I've been trying to solve this with "Load game" tutorials, but I'm unsuccessful so far. This is the code I'm using for loading the file:

Are you trying to set the values in the XML at run time, or calling mutator functions on the objects aren't doing anything? If the latter, that is very odd and I'd have to see more. It's possibly related to your other problem.


This method is a part of a class, and when I call it from the main class I get the NullReferenceException error. I placed a breakpoint, and I can see that all of my variables (rezultat,strem,serializer, postavke) have values null. Do you have some idea how to solve this
[/quote]
My xna is too rusty to handle that (have never used xna 4.0), but I'll give you one decent debugging tip. Start with the minimum size of that function possible and work your way up to what you have. just call BeginOpenContainer and see if you're getting null back, then add on bits as you solve your individual problems.

Share this post


Link to post
Share on other sites

Are you trying to set the values in the XML at run time, or calling mutator functions on the objects aren't doing anything?

The XML file will be changed from another program, I don't need to worry about that, but what I'm asking is how can I apply the changes made to the XML file to my program (since XML contains the values for variables in my program). And that is the problem I'm having - when I change the XML file manually, my program continues to behave as if nothing has changed unless I rebuild the whole solution. Is there a way to set the variable values when program is starting?
I'll try the debugging tip you gave me

Share this post


Link to post
Share on other sites
It's kind of annoying, but a work around for not having to always rebuild your project is if you alter the xml file that is in your debug folder and just run your debug exe. This way you don't have to rebuild your entire project to get the results of a changed xml file. Be careful though! You need to remember to put the xml file you are happy with back into your project in VS and not rebuild the project and overwrite your files!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!