MonoGame Pipeline XML Load error 'Element' is an invalid XmlNodeType

416 views Asked by At

I'm trying to load an XML file into my MonoGame game via the pipeline but I am getting an error.

'Element' is an invalid XmlNodeType. Line 10, position 6.

I have created my classes for the XML File in an external portable class library project and added that DLL to the pipeline content references. But when I try to build the XML file within the MonoGame Pipeline application I get the above error.

Any Ideas?

The XML and Class code are below

MainMenu.xml (I have marked the error line with an xml style comment, comment not in the actual file)

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
  <Asset Type="Menu">
    <Title>Main Menu</Title>
      <Background>
          <Type>animation</Type>
          <Data>MainMenuBackground</Data>
      </Background>
      <Options>
          <Option> <!-- Error Here -->
              <Type>text</Type>
              <Name>new</Name>
              <Disabled>false</Disabled>
              <Text>New Game</Text>
              <Action>newGame</Action>
          </Option>
          <Option>
              <Type>text</Type>
              <Name>save</Name>
              <Disabled>true</Disabled>
              <Text>Save Game</Text>
              <Action>saveGame</Action>
          </Option>
          <Option>
              <Type>text</Type>
              <Name>load</Name>
              <Disabled>false</Disabled>
              <Text>Load Game</Text>
              <Action>loadGame</Action>
          </Option>
          <Option>
              <Type>text</Type>
              <Name>exit</Name>
              <Disabled>false</Disabled>
              <Text>Exit Game</Text>
              <Action>exitGame</Action>
          </Option>
      </Options>
      <Buttons>
          <Keyboard>
              <Accept>Enter</Accept>
              <Cancel>Esc</Cancel>
          </Keyboard>
          <Controller>
              <Accept>A</Accept>
              <Cancel>B</Cancel>
          </Controller>
      </Buttons>
  </Asset>
</XnaContent>

Menu.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLMenu
{
    public class Menu
    {
        public String Title;
        public Background Background = new Background();
        public Option[] Options = new Option[] { };
        public Buttons Buttons = new Buttons();
    }
}

Background.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLMenu
{
    public class Background
    {
        public String Type;
        public String Data;
    }
}

Option.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLMenu
{
    public class Option
    {
        public String Type;
        public String Name;
        public Boolean Disabled;
        public String Text;
        public string Action;
    }
}

Buttons.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLMenu
{
    public class Buttons
    {
        public ControlButtons Keyboard = new ControlButtons();
        public ControlButtons Controller = new ControlButtons();
    }
}

ControlButtons.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XMLMenu
{
    public class ControlButtons
    {
        public String Accept;
        public String Cancel;
    }
}
1

There are 1 answers

4
Shawn Rakowski On BEST ANSWER

The IntermediateSerializer, used to import XML content into XNA/MonoGame projects, expects items in a collection to be tagged with <Item> not <Option> which is why you are receiving the error. There are 2 options for you to resolve this.

The first option is to change every <Option> tag to be an <Item> tag. If you do not already have a reference to the MonoGame assemblies on your XMLMenu assembly and don't wish to add the dependency then this is your option.

The second, and I think better, option is to add a reference to the MonoGame assemblies and add an attribute to the Options array in your Menu class. The ContentSerializer attribute has a constructor argument called CollectionItemName. If you assign "Option" to this parameter the compilation succeeds. I replicated your setup, modified the Menu class to look like this:

using Microsoft.Xna.Framework.Content;
using System;

namespace XMLMenu
{
    public class Menu
    {
        public String Title;
        public Background Background = new Background();
        [ContentSerializer(CollectionItemName = "Option")]
        public Option[] Options = new Option[] { };
        public Buttons Buttons = new Buttons();
    }
}

and succeeded at building the content. Without the ContentSerializer attribute I received the same error as you.

For more information, read the "Collection" and proceeding sections in this article: https://shawnhargreaves.com/blog/everything-you-ever-wanted-to-know-about-intermediateserializer.html