XML simple loading problem

Started by
3 comments, last by comstation11 11 years, 11 months ago
Hello. I am trying to create items for my game by putting their settings in an xml file, but I am having no help. I have had little previous experience with xml, so my mistake is probably a very simple one. To load the settings, I use the Load() method in my Item class. I have added the xml file to my game's content, but it is still not working.

The error is:

Error 1 There was an error while deserializing intermediate XML. 'Element' is an invalid XmlNodeType. Line 6, position 4.

Here is the Item Class:



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using Microsoft.Xna.Framework;


namespace RPG
{
class Item
{

protected string assetName;
protected string description;
protected Texture2D graphicIcon;
protected int goldValue;
protected bool dropable;


public void Load(string asset)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"Content/Items/" + asset + ".xml");

foreach (XmlNode root in doc.ChildNodes)
{
foreach (XmlNode node in root.ChildNodes)
{
if (node.Name == "Asset")
assetName = node.InnerText;

if (node.Name == "Description")
description = node.InnerText;

if (node.Name == "GraphicIcon")
graphicIcon = LoadTexture(node.InnerText);

if (node.Name == "GoldValue")
goldValue = int.Parse(node.InnerText);

if (node.Name == "Dropable")
dropable = bool.Parse(node.InnerText);

}//end child nodes
}

}

protected Texture2D LoadTexture(string asset)
{
return Globals.Content.Load<Texture2D>("" + asset);
}
}
}



and XML code:


<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<!-- TODO: replace this Asset with your own XML asset data. -->
<Asset Type="System.String"></Asset>

<Item>

<Asset>BronzeBand</Asset>
<Description>This worn ring looks rusted</Description>
<GoldValue>1</GoldValue>
<Dropable>true</Dropable>


</Item>


</XnaContent>
Advertisement
What do you mean by "it's not working"? Do you get a compiler error? Runtime error? Just doesn't do what you expect?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Sorry :/ that was a stupid mistake. When I run the program, this is the error I get:

Error 1 There was an error while deserializing intermediate XML. 'Element' is an invalid XmlNodeType. Line 6, position 4.
The error message tells you everything you need to know: on line 6 of your XML file, at column 4, you have an XML node which is invalid. Specifically, <item> is not acceptable.

Are you following a guide or the documentation for XNA serialization for this? If not, might I suggest looking for one? :-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

To have content files that compile to raw class data, you need to follow this tutorial: http://msdn.microsoft.com/en-us/library/ff604979.aspx

Basically, you need to add a project that is referenced by your game project and your content project. Then, in the XML file, you need to change the Asset element's Type attribute to the class's full name i.e "GameDataProject.Item"

Now everything should work!

This topic is closed to new replies.

Advertisement