Jump to content

  • Log In with Google      Sign In   
  • Create Account


XML simple loading problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Kaandimandimanbopbobop   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 May 2012 - 07:31 PM

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>

Edited by Kaandimandimanbopbobop, 24 May 2012 - 08:32 PM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14299

Like
0Likes
Like

Posted 24 May 2012 - 07:40 PM

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

#3 Kaandimandimanbopbobop   Members   -  Reputation: 109

Like
0Likes
Like

Posted 24 May 2012 - 08:24 PM

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.

#4 ApochPiQ   Moderators   -  Reputation: 14299

Like
0Likes
Like

Posted 24 May 2012 - 10:04 PM

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? :-)

#5 comstation11   Members   -  Reputation: 122

Like
0Likes
Like

Posted 25 May 2012 - 12:50 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS