• Advertisement
Sign in to follow this  

XML simple loading problem

This topic is 2067 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

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:

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

Here is the Item Class:


[CODE]
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);
}
}
}

[/CODE]

and XML code:

[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>
[/CODE] Edited by Kaandimandimanbopbobop

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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? :-)

Share this post


Link to post
Share on other sites
To have content files that compile to raw class data, you need to follow this tutorial: [url="http://msdn.microsoft.com/en-us/library/ff604979.aspx"]http://msdn.microsoft.com/en-us/library/ff604979.aspx[/url]

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!

Share this post


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

  • Advertisement