Saving Level in XML - c#

Greetings to everyone. I am creating a Level with objects (Tiles, Obstacles, Character). I am experiencing a problem. The serialization is successfully made, but I get empty Lists. I want to serialize and save the attributes of each object. For example:
public class Obstacle
{
public Texture2D ob_tex;
public Rectangle ob_rec;
public bool ob_clic;
Obstacle() { } // Create Constructor
}
I use this code to save the level:
public class Level
{
public List<Obstacle> obstacles;
public LevelFile levelfile;
public Level()
{
obstacles = new List<Obstacle>();
}
public class LevelFile
{
public List<Obstacle> obstacles;
}
public void Save(String path/*, LevelFile levelfile*/)
{
levelfile = new LevelFile();
levelfile.obstacles = obstacles;
XmlSerializer serializer = new XmlSerializer(typeof(LevelFile));
using (StreamWriter streamWriter = new StreamWriter(path))
{
serializer.Serialize(streamWriter, levelfile);
}
}
}
But I get an xml file which is like this:
<LevelFile<obstacles /></LevelFile>
And nothing in it(Rectangle value, Texture and bool)....

Well, according to your code you're saving a new LevelFile() which has an obstacles member that's a list, which you set from the obstacles member in your Level() constructor, which is an empty list. Thus the XML is right, you are outputting a file with an empty list which shows as the empty tag.
So it looks like your code is correct. If you add an obstacle to your list in the constructor or save, you'll see that it's actually working correctly:
public Level()
{
obstacles = new List<Obstacle>
{
new Obstacle { /* set your properties here */ }
};
}

Related

Unusual XML Format I Must Iterate Through

I need to iterate through some XML that has an unusual format to it. It looks like this:
<Baseball>
<Player playerID="123" playerName="John Smith" playerBats="Right"/>
<position positionID="1b" positionCode="abc" counter="3"/>
<position positionID="2b" positionCode="def" counter="2"/>
</Player>
</Baseball>
I cannot change this format that is given to me. I need to iterate through every line and different pieces get pulled and go to different places. I will be doing the code in C#. Ideas? Thank you!
Assuming your input XML actually IS valid XML, this is the pattern I use for this type of thing.
Your example XML isn't valid because Player is both self closing, and has an explicit closing tag. I've adjusted it to my best guess of what it should look like.
If that really is the XML you have to deal with, XmlDocument.LoadXml will throw an error. You will need to find some other way to deal with the malformed data, perhaps pre-processing the data to remove the / on the Player element so it is no longer self closing.
The basic pattern is that there is a class for each element in the XML. Each class has a static function FromXml that accepts an XmlElement for the matching element in the XML. FromXML is responsible for reading, parsing, and populating its properties from attributes. Child elements are processed by calling FromXml on their associated classes.
class Program
{
static void Main(string[] args)
{
string xml =
#"<Baseball>
<Player playerID=""123"" playerName=""John Smith"" playerBats=""Right"">
<position positionID=""1b"" positionCode=""abc"" counter=""3""/>
<position positionID=""2b"" positionCode=""def"" counter=""2""/>
</Player>
</Baseball>";
var document = new XmlDocument();
document.LoadXml(xml);
var players = new List<Player>();
foreach (XmlElement baseballElement in document.SelectNodes("Baseball"))
{
foreach (XmlElement playerElement in baseballElement.SelectNodes("Player"))
{
players.Add(Player.FromXml(playerElement));
}
}
Console.ReadLine();
}
}
public class Player
{
public static Player FromXml(XmlElement PlayerElement)
{
var player = new Player();
player.PlayerId = int.Parse(PlayerElement.GetAttribute("playerID"));
player.PlayerName = PlayerElement.GetAttribute("playerName");
player.PlayerBats = PlayerElement.GetAttribute("playerBats");
foreach (XmlElement positionElement in PlayerElement.SelectNodes("position"))
{
player.Positions.Add(Position.FromXml(positionElement));
}
return player;
}
public int PlayerId { get; set; }
public string PlayerName { get; set; }
public string PlayerBats { get; set; }
private List<Position> _positions = new List<Position>();
public List<Position> Positions
{
get { return _positions; }
}
}
public class Position
{
public static Position FromXml(XmlElement positionElement)
{
var position = new Position();
position.PositionId = positionElement.GetAttribute("positionID");
position.PositionCode = positionElement.GetAttribute("positionCode");
position.Counter = int.Parse(positionElement.GetAttribute("counter"));
return position;
}
public string PositionId { get; set; }
public string PositionCode { get; set; }
public int Counter { get; set; }
}
This will create a list of Player each of which contains a list of Position, all populated from the XML.
I also haven't done any kind of error checking on the input XML. If any attributes are missing or in the wrong format, it will throw an error.

Why is my game serializing this class?

So I'm making a game, and it saves users' progress on the computer in a binary file. The User class stores a few things:
Integers for stat values (Serializable)
Strings for the Username and the skin assets
Lists of both the Achievement class and the InventoryItem class, which I have created myself.
Here are the User fields:
public string Username = "";
// ID is used for local identification, as usernames can be changed.
public int ID;
public int Coins = 0;
public List<Achievement> AchievementsCompleted = new List<Achievement>();
public List<InventoryItem> Inventory = new List<InventoryItem>();
public List<string> Skins = new List<string>();
public string CurrentSkinAsset { get; set; }
The Achievement class stores ints, bools, and strings, which are all serializable. The InventoryItem class stores its name (a string) and an InventoryAction, which is a delegate that is called when the item is used.
These are the Achievement class's fields:
public int ID = 0;
public string Name = "";
public bool Earned = false;
public string Description = "";
public string Image;
public AchievmentDifficulty Difficulty;
public int CoinsOnCompletion = 0;
public AchievementMethod OnCompletion;
public AchievementCriteria CompletionCriteria;
public bool Completed = false;
And here are the fields for the InventoryItem class:
InventoryAction actionWhenUsed;
public string Name;
public string AssetName;
The source of the InventoryAction variables are in my XNAGame class. What I mean by this is that the XNAGame class has a method called "UseSword()" or whatever, which it passes into the InventoryItem class. Previously, the methods were stored in the Game1 class, but the Game class, which Game1 inherits from, is not serializable, and there's no way for me to control that. This is why I have an XNAGame class.
I get an error when trying to serialize: "The 'SpriteFont' class is not marked as serializable", or something like that. Well, there is a SpriteFont object in my XNAGame class, and some quick tests showed that this is the source of the issue. Well, I have no control over whether or not the SpriteFont class is Serializable.
Why is the game doing this? Why must all the fields in the XNAGame class be serializable, when all I need is a few methods?
Keep in mind when answering that I'm 13, and may not understand all the terms you're using. If you need any code samples, I'll be glad to provide them for you. Thanks in advance!
EDIT: One solution I have thought of is to store the InventoryAction delegates in a Dictionary, except that this will be a pain and isn't very good programming practice. If this is the only way, I'll accept it, though (Honestly at this point I think this is the best solution).
EDIT 2: Here's the code for the User.Serialize method (I know what I'm doing in inefficient, and I should use a database, blah, blah, blah. I'm fine with what I'm doing now, so bear with me.):
FileStream fileStream = null;
List<User> users;
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
if (File.Exists(FILE_PATH) && !IsFileLocked(FILE_PATH))
{
fileStream = File.Open(FILE_PATH, FileMode.Open);
users = (List<User>)binaryFormatter.Deserialize(fileStream);
}
else
{
fileStream = File.Create(FILE_PATH);
users = new List<User>();
}
for (int i = 0; i < users.Count; i++)
{
if (users[i].ID == this.ID)
{
users.Remove(users[i]);
}
}
foreach (Achievement a in AchievementsCompleted)
{
if (a.CompletionCriteria != null)
{
a.CompletionCriteria = null;
}
if (a.OnCompletion != null)
{
a.OnCompletion = null;
}
}
users.Add(this);
fileStream.Position = 0;
binaryFormatter.Serialize(fileStream, users);
You cannot serialize a SpriteFont by design, actually this is possible (.XNB file) but it hasn't been made public.
Solution:
Strip it off your serialized class.
Alternatives:
If for some reasons you must serialize some font, the first thing that comes to my mind would be to roll-out your own font system such as BMFont but that's a daunting task since you'll have to use it everywhere else where you might already do ...
Generate a pre-defined amount of fonts (i.e. Arial/Times/Courier at size 10/11/12 etc ...) using XNA Content app (can't recall its exact name); then store this user preference as two strings. With a string.Format(...) you should be able to load the right font back quite easily.
Alternative 2 is certainly the easiest and won't take more than a few minutes to roll-out.
EDIT
Basically, instead of saving a delegate I do the following:
inventory items have their own type
each type name is de/serialized accordingly
their logic does not happen in the main game class anymore
you don't have to manually match item type / action method
So while you'll end up with more classes, you have concerns separated and you can keep your main loop clean and relatively generic.
Code:
public static class Demo
{
public static void DemoCode()
{
// create new profile
var profile = new UserProfile
{
Name = "Bill",
Gold = 1000000,
Achievements = new List<Achievement>(new[]
{
Achievement.Warrior
}),
Inventory = new Inventory(new[]
{
new FireSpell()
})
};
// save it
using (var stream = File.Create("profile.bin"))
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, profile);
}
// load it
using (var stream = File.OpenRead("profile.bin"))
{
var formatter = new BinaryFormatter();
var deserialize = formatter.Deserialize(stream);
var userProfile = (UserProfile) deserialize;
// set everything on fire :)
var fireSpell = userProfile.Inventory.Items.OfType<FireSpell>().FirstOrDefault();
if (fireSpell != null) fireSpell.Execute("whatever");
}
}
}
[Serializable]
public sealed class UserProfile
{
public string Name { get; set; }
public int Gold { get; set; }
public List<Achievement> Achievements { get; set; }
public Inventory Inventory { get; set; }
}
public enum Achievement
{
Warrior
}
[Serializable]
public sealed class Inventory : ISerializable
{
public Inventory() // for serialization
{
}
public Inventory(SerializationInfo info, StreamingContext context) // for serialization
{
var value = (string) info.GetValue("Items", typeof(string));
var strings = value.Split(';');
var items = strings.Select(s =>
{
var type = Type.GetType(s);
if (type == null) throw new ArgumentNullException(nameof(type));
var instance = Activator.CreateInstance(type);
var item = instance as InventoryItem;
return item;
}).ToArray();
Items = new List<InventoryItem>(items);
}
public Inventory(IEnumerable<InventoryItem> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
Items = new List<InventoryItem>(items);
}
public List<InventoryItem> Items { get; }
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
var strings = Items.Select(s => s.GetType().AssemblyQualifiedName).ToArray();
var value = string.Join(";", strings);
info.AddValue("Items", value);
}
#endregion
}
public abstract class InventoryItem
{
public abstract void Execute(params object[] objects);
}
public abstract class Spell : InventoryItem
{
}
public sealed class FireSpell : Spell
{
public override void Execute(params object[] objects)
{
// using 'params object[]' a simple and generic way to pass things if any, i.e.
// var world = objects[0];
// var strength = objects[1];
// now do something with these !
}
}
Okay, so I figured it out.
The best solution was to use a Dictionary in the XNAGame class, which stores two things: an ItemType (an enumeration), and an InventoryAction. Basically, when I use an item, I check it's type and then look up it's method. Thanks to everyone who tried, and I'm sorry if the question was confusing.

How to bind an object to a list?

This is not a windows form app, I'm not sure I'm using the correct terminology. I'm trying to bind an object to a list so when the object is modified outside of the list, those changes are reflected in the list. I'm not entirely sure how to start, my searches just keep returning "winform" answers to a datasource but this is not what I want. Here is what I have so far:
You can copy this code into a console app if you wish to test it. Notice the foreach that loops through go.Getcomponents() does not show the names because I don't think the object modified is still referenced when pulled out of the list. Essentially I'm trying to modify the object outside the list but when that object is modified the object in the list is also modified.
It is important that it can be serialized because the GameObject will be transferred across a network and data within it will be read by a server.
class Test
{
public void TestStart()
{
GameObject go = new GameObject(); //create GameObject
Dog dog = go.AddComponent<Dog>(); //Add a Dog component to the GameObject
dog.name = "Fluffy"; //name the dog fluffy, this should be reflected in the GenericComponent list of GameObject
Dog dog2 = go.AddComponent<Dog>();
dog2.name = "Fuzzy";
//loop through all dog components in GameObject go, doesn't print dog names :(
foreach (Dog dg in go.GetComponents<Dog>())
{
Console.WriteLine(dg.name);
}
Console.ReadLine();
}
}
[Serializable]
public class GameObject
{
List<GenericComponent<Object>> componentList = new List<GenericComponent<Object>>();
//returns first found in list.
public T GetComponent<T>()
{
return (T)componentList.Find(c => c.component.GetType() == typeof(T)).component;
}
//add a component to component list.
public T AddComponent<T>()
{
GenericComponent<Object> newComponent = new GenericComponent<Object>();//;(T)Activator.CreateInstance(typeof(T));
newComponent.component = (T)Activator.CreateInstance(typeof(T));
componentList.Add(newComponent);
return (T)Activator.CreateInstance(typeof(T));
}
//returns all components of type T
public List<T> GetComponents<T>()
{
List<T> r = new List<T>();
for (int i = 0; i < componentList.Count; i++)
{
if (componentList[i].component.GetType() == typeof(T))
{
r.Add((T)componentList[i].component);
}
}
return r;
}
}
[Serializable]
public class GenericComponent<T> where T : new()
{
public T component;
public GenericComponent()
{
component = new T();
}
}
[Serializable]
public class Dog
{
public string name = "";
public Dog() { }
public Dog(string name)
{
this.name = name;
}
}
In your AddComponent method, you are adding one component and then returning another one. Instead, return the same one you added:
public T AddComponent<T>()
{
GenericComponent<Object> newComponent = new GenericComponent<Object>();
newComponent.component = (T)Activator.CreateInstance(typeof(T));
componentList.Add(newComponent);
return (T)newComponent.component;
}

How to make XmlSerializer.Deserialize handle DefaultAttribute properly?

There seems to be a bug / inconsistency in the Microsoft XmlSerializer: If you have a property marked with a System.ComponentModel.DefaultValue attribute, this does not get serialized. Fair enough - this could be seen as an expected behavior.
The problem is the same attribute is not respected when deserializing. The code below illustrates the issue.
Question is how could I bypass this? I have potentially hundreds of business classes with default values used in the UI tier (Views), so default value initialization in constructor is not an option. It has to be something generic. I could create a completely new default attribute, but it seems like duplicate work. Do you see a way to override the XmlSerializer behavior or should I use just another serializer that does the job better?
The example code:
public class DefaultValueTestClass
{
[System.ComponentModel.DefaultValue(10000)]
public int Foo { get; set; }
}
[TestMethod]
public void SimpleDefaultValueTest()
{
// Create object and set the property value TO THE DEFAULT
var before = new DefaultValueTestClass();
before.Foo = 10000;
// Serialize => xml
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(DefaultValueTestClass));
string xml;
using (var stream = new System.IO.StringWriter())
{
serializer.Serialize(stream, before);
xml = stream.ToString();
}
// Deserialize the same object
DefaultValueTestClass after;
using (var reader = new System.IO.StringReader(xml))
{
after = (DefaultValueTestClass)serializer.Deserialize(reader);
}
// before.Foo = 10000
// after.Foo = 0
Assert.AreEqual(before.Foo, after.Foo);
}
It is your job to implement the defaults; [DefaultValue] merely says "this is the default, you don't need to worry about this" - it doesn't apply it. This applies not just to XmlSerializer, but to the core System.ComponentModel API to which [DefaultValue] belongs (which drives things like the bold / not-bold in PropertyGrid, etc)
Basically:
public class DefaultValueTestClass
{
public DefaultValueTestClass()
{
Foo = 10000;
}
[DefaultValue(10000)]
public int Foo { get; set; }
}
will work in the way you expect. If you want it to serialize whether or not it is that particular value, then the correct implementations is:
public class DefaultValueTestClass
{
public DefaultValueTestClass()
{
Foo = 10000;
}
public int Foo { get; set; }
}
If you want to preserve the [DefaultValue], but want it to always serialize, then:
public class DefaultValueTestClass
{
[DefaultValue(10000)]
public int Foo { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeFoo() { return true; }
}
Where ShouldSerialize* is another pattern from System.ComponentModel that is recognised by several serializers.
And here's some UI code to show that XmlSerializer is actually doing exactly the same things that the UI code (built on System.ComponentModel) has always done:
using System.ComponentModel;
using System.Windows.Forms;
static class Program
{
[System.STAThread]
static void Main()
{
Application.EnableVisualStyles();
using (var form = new Form())
using (var grid = new PropertyGrid())
{
grid.Dock = DockStyle.Fill;
var obj = new DefaultValueTestClass
{ // TODO - try with other numbers to
// see bold / not bold
Foo = 10000
};
// note in the grid the value is shown not-bold; that is
// because System.ComponentModel is saying
// "this property doesn't need to be serialized"
// - or to show it more explicitly:
var prop = TypeDescriptor.GetProperties(obj)["Foo"];
bool shouldSerialize = prop.ShouldSerializeValue(obj);
// ^^^ false, because of the DefaultValueAttribute
form.Text = shouldSerialize.ToString(); // win title
grid.SelectedObject = obj;
form.Controls.Add(grid);
Application.Run(form);
}
}
}
public class DefaultValueTestClass
{
[System.ComponentModel.DefaultValue(10000)]
public int Foo { get; set; }
}

C# Serialize a class with a List data member

I have this c# class:
public class Test
{
public Test() { }
public IList<int> list = new List<int>();
}
Then I have this code:
Test t = new Test();
t.list.Add(1);
t.list.Add(2);
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
StringWriter sw = new StringWriter();
XmlSerializer xml = new XmlSerializer(t.GetType());
xml.Serialize(sw, t);
When I look at the output from sw, its this:
<?xml version="1.0" encoding="utf-16"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
the values 1,2 I added to the list member variable dont show up.
So how can I fix this ? I made the list a property but it still doesnt seem to work.
I am using xml serialization here, are there any other serializers ?
I want performance! Is this the best approach ?
--------------- UPDATE BELOW -------------------------
So the actual class I want to serialize is this:
public class RoutingResult
{
public float lengthInMeters { get; set; }
public float durationInSeconds { get; set; }
public string Name { get; set; }
public double travelTime
{
get
{
TimeSpan timeSpan = TimeSpan.FromSeconds(durationInSeconds);
return timeSpan.TotalMinutes;
}
}
public float totalWalkingDistance
{
get
{
float totalWalkingLengthInMeters = 0;
foreach (RoutingLeg leg in Legs)
{
if (leg.type == RoutingLeg.TransportType.Walk)
{
totalWalkingLengthInMeters += leg.lengthInMeters;
}
}
return (float)(totalWalkingLengthInMeters / 1000);
}
}
public IList<RoutingLeg> Legs { get; set; } // this is a property! isnit it?
public IList<int> test{get;set;} // test ...
public RoutingResult()
{
Legs = new List<RoutingLeg>();
test = new List<int>(); //test
test.Add(1);
test.Add(2);
Name = new Random().Next().ToString(); // for test
}
}
But the XML produced by the serializer is this:
<RoutingResult>
<lengthInMeters>9800.118</lengthInMeters>
<durationInSeconds>1440</durationInSeconds>
<Name>630104750</Name>
</RoutingResult>
???
its ignoring both of those lists ?
1) Your list is a field, not a property, and the XmlSerializer will only work with properties, try this:
public class Test
{
public Test() { IntList = new List<int>() }
public IList<int> IntList { get; set; }
}
2) There are other Serialiation options, Binary the main other one, though there is one for JSON as well.
3) Binary is probably the most performant way, since it is typically a straight memory dump, and the output file will be the smallest.
list is not a Property. Change it to a publicly visible property, and it should be picked up.
I figured it out that XmlSerializer doesnt work if I use IList so I changed it to List, that made it work. As Nate also mentioned.

Categories

Resources