Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an XML file via the Monogame content pipeline?

Tags:

c#

xml

xna

monogame

I am currently trying to port a game I am working on from XNA to Monogame, but I am having some trouble getting the content pipeline to cooperate. My game uses a number of XML files as XNB assets to represent objects in the game, which I created following the instructions here. However, trying to move this across word-for-word into Monogame produces the following error:

An unhandled exception of type 'Microsoft.Xna.Framework.Content.ContentLoadException' occurred in MonoGame.Framework.dll

Additional information: Could not load Parts\Window.xnb asset as a non-content file!

Here is the class I have been using to define the content of the XML files:

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

namespace FileTypes
{
    public class PartFile
    {
        public struct State
        {
            public Rectangle[] frames;
            public int[] collision;
        }

        public Vector2 size;
        public string image;
        public string defaultState = "default";
        public bool fillBG;
        public int ticksPerFrame;
        public Dictionary<string, State> states;
        public string[] modules;
    }
}

Here is one of the XML files in the project:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="FileTypes.PartFile">
    <size>2 3</size>
    <image>Computer</image>
    <defaultState>default</defaultState>
    <fillBG>false</fillBG>
    <ticksPerFrame>7</ticksPerFrame>
    <states>
      <Item>
        <Key>default</Key>
        <Value>
          <frames>
            0 0 40 60
            40 0 40 60
          </frames>
          <collision>
            0 0
            0 0
            0 0
          </collision>
        </Value>
      </Item>
      <Item>
        <Key>active</Key>
        <Value>
          <frames>
            80 0 40 60
            120 0 40 60
          </frames>
          <collision>
            0 0
            0 0
            0 0
          </collision>
        </Value>
      </Item>
    </states>
    <modules>
      <Item>testModule</Item>
    </modules>
  </Asset>
</XnaContent>

And finally here is a sample of the code I use to load the file:

FileTypes.PartFile srcPart = content.Load<FileTypes.PartFile>("Parts\\" + name);

Does anybody know what I need to do in order to get my code working in Monogame? I've been looking around the internet for a fair while, but so far I've yet to find a solution to my issue. Alternatively, if I've been going about the entire system wrong all this time and there's a far easier way to handle what I want to do, I'd love to hear it.

Thanks in advance!

like image 580
Hunter X Avatar asked Feb 19 '14 18:02

Hunter X


1 Answers

I wrote a rather long tutorial about loading custom content with the MonoGame Pipeline. Here's the summary

  1. Create a new project to hold your content importer, processor and writer
  2. Create the content importer
  3. Create the content processor
  4. Create the content type writer
  5. Create the content type reader
  6. Reference the DLL from the Pipeline tool
  7. Read the content into your game

Creating the importer, processor, reader and writer is the same as XNA. You can refer to the MSDN documentation for that.

The tricky part is getting your DLL to work with the Pipeline tool. To add it as a reference look for the the References property of the root tree node.

like image 91
craftworkgames Avatar answered Oct 13 '22 23:10

craftworkgames