-
|
|
|
can anyone just tell me what the simplest way is to load some data (strings and integers) out of an xml document? I've been looking around for the last 2 hours and i can only seem to find stuff for Xna 1.0, and my game runs on 2.0
|
|
-
|
|
|
I haven't done much with XML yet, but I'll be using it soon, and I was told to look into XPath or XSLT
|
|
-
-
|
|
|
while that is something i wanted to know, that isn't quite what i ment. What I need to do is to read alot of data from a few xml files, basically the data is strings, and ints. what i want to do is say something like:
<unit>
<name> "Infantry" </name>
<maxHp> 10 </maxHp>
</unit>
and crap similar to that. What i've seen in some examples is people calling stuff like (xmlfilename).unit.maxHP;
|
|
-
-
|
|
|
I guess i'm just not understanding how exactly to use serialization to do what i need. Can you make a small example program?(one that just loads a could stings and integers from a XML file) I tried to understand what they were doing in the example the link provided, but it seemed like they were just making a new struct rather than loading data from a xml file.
|
|
-
-
- (1547)
-
premium membership
-
Posts
462
|
|
Hex21189:I guess i'm just not understanding how exactly to use serialization to do what i need. Can you make a small example program?(one that just loads a could stings and integers from a XML file) I tried to understand what they were doing in the example the link provided, but it seemed like they were just making a new struct rather than loading data from a xml file.
Are the XML files set in stone, or do you have the ability to set/modify the structure? If set in stone, you may need to parse it manually. If you can create the schema for the file, just make a serializable struct or class to hold the values you need (arrays work fine if needed). Then, you can just serialize/deserialize the data structure as necessary.
|
|
-
|
|
|
for now they can be permanent, but later i'd like add features for the players to change them (like making custom units, or changing existing ones), but still have no idea what exactly it means to serialize/deserialize anything (never actually used XML before, only java/c#/QBasic/VBasic)
|
|
-
-
- (1547)
-
premium membership
-
Posts
462
|
|
Hex21189:for now they can be permanent, but later i'd like add features for the players to change them (like making custom units, or changing existing ones), but still have no idea what exactly it means to serialize/deserialize anything (never actually used XML before, only java/c#/QBasic/VBasic)
What I meant by being "permanent" was in regards to your ability to change the files. If the XML files were created by another program or were from an external source, then the structure could be considered permanent. However, since these are unit definitions, then you should be able to construct the XML files any way that you want.
Here is what I do for simple constructs (all based off of various sources floating around), let's say an XML file containing UI layout settings for a particular display resolution. First, I create the actual data structure in code:
| [Serializable] |
| public struct Resolution |
| { |
| public int borderWidth; |
| public Color borderColor; |
| public string[ captions; |
| } |
Somewhere in code (most likely a separate project in the same solution), I will create a few of the objects, populate them with settings and serialize it to an actual XML file. Of course, you could also just create the base object and populate it manually later too. Here is how I dump out a basic XML file:
| Resolution outputResolution; |
| // Populate data somewhere |
| FileStream file; |
| file = File.Open(fileName, FileMode.CreateNew); |
| XmlSerializer serializer = new XmlSerializer(typeof(Resolution)); |
| serializer.Serialize(file, outputResolution); |
| file.Close(); |
| |
Then, to read in the XML files created like this:
| FileStream file; |
| file = File.Open(fileName, FileMode.Open, FileAccess.Read); |
| XmlSerializer serializer = new XmlSerializer(typeof(Resolution)); |
| Resolution inputResolution = (Resolution)serializer.Deserialize(file); |
| file.Close(); |
| |
All you have to do is replace the casts as necessary for your data structure(s). Hopefully this will give you something to start with.
|
|
-
|
|
|
do i have to make a structure? I was hoping i could just type something in and it would give me a string or something from the xml file.
|
|
-
-
- (1547)
-
premium membership
-
Posts
462
|
|
Hex21189:do i have to make a structure? I was hoping i could just type something in and it would give me a string or something from the xml file.
I think that you need to be more specific in what you are trying to do. There are no commands, that I am aware of, that you could use to just blindly pull data from an XML file. The effect of using a class or struct is to load the XML file into memory so that you have the data in a usable format (the class/struct). Alternately, you could read the XML file in line by line or piece by piece, but you would need to use the more sinister XML manipulation techniques. Regardless, you must load the XML in some way before you can use it.
I honestly don't see why you are trying to avoid structs. You mentioned earlier about how you wanted to address items like this: (xmlfilename).unit.maxHP. Well, replace (xmlfilename) with the name of your struct and make sure that the "unit" is a named member of this struct and you have exactly what you are looking to do.
|
|
-
|
|
|
i mainly dont want to use structs because i already programed my game, and now i'm just trying to make it more efficient. I actually am using a general unit class, and i wanted to pull the xml data (names, amount of hp, damage, etc) into it when i create a new unit. (sorry i didnt mention that sooner, i figuered it was irrelevant, what i'm really looking for is just a functioning example that i can just load into XNA and run to see how exactly it works.)
|
|
-
-
- (1373)
-
premium membership
-
Posts
976
|
|
look at XmlTextReader.
you can just..
XmlTextReader reader = new XmlTextReader(file);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
if (reader.Name.Equals("texture"))
{
while (reader.MoveToNextAttribute())
{
if (reader.Name.Equals("name"))
{
Produces a mess of if statements, but does work.
Don't think it's available on the xbox, but could be wrong
Information is not knowledge, knowledge is not wisdom, wisdom is not truth, truth is not beauty, beauty is not love, love is not music, music is the best! Wisdom is the domain of the Wis (which is extinct).
|
|
-
|
|
|
Hex21189:do i have to make a structure? I was hoping i could just type something in and it would give me a string or something from the xml file.
Either make a new one, use one you already have, or parse the XML manually.
|
|
-
|
|
|
Nick's Tileset Tutorial has an XML tutorial included that may be able to provide you with the functionality you are looking for. You will have to use the concepts behind it to drive your needs, but it should help you.
|
|
-
|
|
|
well i still don't fully get it, but i think i did took at a XNA book in Barnes & Noble on it (didn't help much), and it seems that you all are right, and that i have to make a reader, and a writer class so theres no avoiding that. But I guess I'm just not going to worry about it right now, since the tile engine tutorial videos look like they are making a similar switch as to what i am doing (mine is a 3D tile based game, and is coded kind of hapahazardly, rather than a 2D tile based game) so i think I'll just watch that whole series and try and rewrite my code and hopefully i'll understand the XML process better by the end of it. Thanks for the help.
|
|
-
|
|
|
Hex21189:well i still don't fully get it, but i think i did took at a XNA book in Barnes & Noble on it (didn't help much), and it seems that you all are right, and that i have to make a reader, and a writer class so theres no avoiding that. But I guess I'm just not going to worry about it right now, since the tile engine tutorial videos look like they are making a similar switch as to what i am doing (mine is a 3D tile based game, and is coded kind of hapahazardly, rather than a 2D tile based game) so i think I'll just watch that whole series and try and rewrite my code and hopefully i'll understand the XML process better by the end of it. Thanks for the help.
I am also writing a 3D tile based engine and used Nick's tutorial as my first step. Most of the concepts can remain the same, but instead of loading, updating, and drawing sprites, you are loading, updating, and drawing models. There really is no way to avoid having to load content from another source (such as XML) unless you hard code everything. But you will likely have to save content as well, so it's going to be necessary to learn it. It's best to jump in early when the things you have to load and save are simplistic.
|
|
-
-
- (0)
-
premium membership
-
Posts
38
|
|
I am using XML to load enemy data into my game engine as well. It does require writing a content reader and writer for my Enemy class which is not a big deal. Then I can just use the content manager to load a list of enemies from my XML. Here is how I did it.
Content Writer which converts an xml to xnb:
| protected override void Write(ContentWriter output, Enemy value) |
| { |
| output.Write(value.Size); |
| output.WriteObject<Vector3>(value.Position); |
| output.WriteObject<Matrix>(value.Orientation); |
| output.Write(value.ModelName); |
| |
| output.WriteObject<Curve>(value.PathXZ); |
| output.WriteObject<Curve>(value.PathYZ); |
| output.Write(value.Stationary); |
| output.Write(value.Airborne); |
| output.Write(value.AimForward); |
| output.Write(value.ShootTimer); |
| output.Write(value.Life); |
| output.Write(value.PropBool); |
| output.Write(value.PointForward); |
| output.Write(value.ConcurrentShots); |
| output.Write(value.Speed); |
| output.Write(value.ShotType); |
| } |
Content Reader which reads the xnb file at runtime:
| protected override Enemy Read(ContentReader input, Enemy existingInstance) |
| { |
| Enemy ret = new Enemy(); |
| ret.Size = input.ReadSingle(); |
| ret.Position = input.ReadObject<Vector3>(); |
| ret.Orientation = input.ReadObject<Matrix>(); |
| ret.ModelName = input.ReadString(); |
| |
| ret.PathXZ = input.ReadObject<Curve>(); |
| ret.PathYZ = input.ReadObject<Curve>(); |
| ret.Stationary = input.ReadBoolean(); |
| ret.Airborne = input.ReadBoolean(); |
| ret.AimForward = input.ReadBoolean(); |
| ret.ShootTimer = input.ReadSingle(); |
| ret.Life = input.ReadInt32(); |
| ret.PropBool = input.ReadBoolean(); |
| ret.PointForward = input.ReadBoolean(); |
| ret.ConcurrentShots = input.ReadInt32(); |
| ret.Speed = input.ReadInt32(); |
| ret.ShotType = input.ReadInt32(); |
| return ret; |
| } |
And finally my call to the content loader in my Game1.cs:
| enemyLibrary = Content.Load<List<Enemy>>("Enemies\\" + "EnemyLibrary"); |
I can post my xml file or my enemy class if you need me to. I had some trouble implementing it too because I was new to XML handling but it works great now.
|
|
-
|
|
|
ah well if you still have the code then please post it (both classes please). more or less what your ding looks like what i'm trying to do, the only difference is that i just want mine to read the data (uses that to make some XNA structs), rather than writing it, although i was thinking of making a unit editor so i guess i'd need to put a writer in anyway
|
|
-
-
- (0)
-
premium membership
-
Posts
38
|
|
Hex21189:ah well if you still have the code then please post it (both classes please). more or less what your ding looks like what i'm trying to do, the only difference is that i just want mine to read the data (uses that to make some XNA structs), rather than writing it, although i was thinking of making a unit editor so i guess i'd need to put a writer in anyway
I think you might be misunderstanding the purpose of the content
writer and reader. The content writer reads the XML file at build time
and outputs it as an xnb file. Then your content reader reads it in at
runtime. I am sure there is a way to parse through the XML file
yourself but I think it is more trouble than it is worth. I would
check the help (F1) to get your bearings straight on the content
pipeline first. Here is my code, I hope it helps.
| [Serializable] |
| public class Enemy// : Microsoft.Xna.Framework.DrawableGameComponent |
| { |
| public float Size = 1.0f; |
| public Vector3 Position = Vector3.Zero; |
| public Matrix Orientation = Matrix.Identity; |
| public String ModelName = String.Empty; |
| |
| //Game Related Assets |
| public Curve PathXZ = new Curve(); |
| public Curve PathYZ = new Curve(); |
| public bool Stationary = true; |
| public bool Airborne = true; |
| public bool AimForward = true; |
| public float ShootTimer = 0.0f; |
| public int Life = 1; |
| public bool PropBool = false; |
| public bool PointForward = false; |
| public int ConcurrentShots = 1; |
| public int Speed = 10; |
| public int ShotType = 0; |
| |
| [ContentSerializerIgnore] |
| public Model Mesh; |
| |
| [ContentSerializerIgnore] |
| public bool Selected = false; |
| |
| [ContentSerializerIgnore] |
| public static Terrain terrain; |
| |
| [ContentSerializerIgnore] |
| public static int MAX_CONCURRENT_SHOTS = 10; |
| |
| [ContentSerializerIgnore] |
| public static int MAX_SHOT_TYPE = 5; |
| } |
| <?xml version="1.0" encoding="utf-8" ?> |
| <XnaContent> |
| <!-- TODO: replace this Asset with your own XML asset data. --> |
| <Asset Type="System.Collections.Generic.List[GameClasses.Enemy]"> |
| <Item> |
| <Size>1</Size> |
| <Position>0 0 0</Position> |
| <Orientation>1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</Orientation> |
| <ModelName>Cube</ModelName> |
| <PathXZ> |
| <PreLoop>Constant</PreLoop> |
| <PostLoop>Constant</PostLoop> |
| <Keys /> |
| </PathXZ> |
| <PathYZ> |
| <PreLoop>Constant</PreLoop> |
| <PostLoop>Constant</PostLoop> |
| <Keys /> |
| </PathYZ> |
| <Stationary>true</Stationary> |
| <Airborne>true</Airborne> |
| <AimForward>true</AimForward> |
| <ShootTimer>0</ShootTimer> |
| <Life>10</Life> |
| <PropBool>false</PropBool> |
| <PointForward>false</PointForward> |
| <ConcurrentShots>1</ConcurrentShots> |
| <Speed>10</Speed> |
| <ShotType>0</ShotType> |
| </Item> |
| <Item> |
| <Size>1</Size> |
| <Position>0 0 0</Position> |
| <Orientation>1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</Orientation> |
| <ModelName>Sphere</ModelName> |
| <PathXZ> |
| <PreLoop>Constant</PreLoop> |
| <PostLoop>Constant</PostLoop> |
| <Keys /> |
| </PathXZ> |
| <PathYZ> |
| <PreLoop>Constant</PreLoop> |
| <PostLoop>Constant</PostLoop> |
| <Keys /> |
| </PathYZ> |
| <Stationary>true</Stationary> |
| <Airborne>true</Airborne> |
| <AimForward>true</AimForward> |
| <ShootTimer>0</ShootTimer> |
| <Life>10</Life> |
| <PropBool>true</PropBool> |
| <PointForward>false</PointForward> |
| <ConcurrentShots>1</ConcurrentShots> |
| <Speed>10</Speed> |
| <ShotType>0</ShotType> |
| </Item> |
| </Asset> |
| </XnaContent> |
| |
|
|
|