1
\$\begingroup\$

Im making a game in XNA 4 and this is the first time I'm using the Content loader to initialize a simple class with a XML file, but no matter how many guide I follow, or how simple or complicated is my XML File the ContentManager cant find the file; the Debug keep telling me: "A first chance exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in Microsoft.Xna.Framework.dll". I'm really confuse because I can load SpriteFonts and Texture2D without a problem ...

I create the following XML (the most basic Xna XML):

    <?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="System.String">Hello</Asset>
</XnaContent>

and I try to load it in the LoadContent method in my main class like this:

System.String hello = Content.Load<System.String>("NewXmlFile");

There is something I'm doing wrong? I really appreciate your help

\$\endgroup\$
  • \$\begingroup\$ In the properties of your XML file, is the importer set correctly? \$\endgroup\$ – me-- Oct 25 '13 at 2:11
  • 2
    \$\begingroup\$ When asking about exceptions, as well as the type of the exception, please provide the Message property of the exception. For example, I can get this exception type by setting the filename incorrectly, with a message: "Error loading "NewXmlFile". File not found.". But I can't be sure that's the same exception you are getting! \$\endgroup\$ – Andrew Russell Oct 25 '13 at 10:38
1
\$\begingroup\$

The code you have provided works fine.

Create a new XNA 4.0 Windows project. Right click on the Content project and add a new item. Choose "XML File". Open that created file and fill it with the content you have in your question.

Then add the line:

System.String hello = Content.Load<System.String>("NewXmlFile");

To your LoadContent method. Be sure that NewXmlFile is the filename of the XML file you created, without its file extension. For example, if you used the default name given to you when creating the file, above, you should replace "NewXmlFile" with "XMLFile1".

Set a breakpoint on the line after your call to Load. Run your game, and observe in the debugger that the variable hello now contains your string.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.