XML Importer - System.ArgumentNullException: Value cannot be null.

Hi all,

I have a problem with XML content. I have the following class:

public class StyleProperty
    {
        /// <summary>Color value.</summary>
        public Color? _color = null;
        public Color asColor { get { return (Color)_color; } }

        /// <summary>Vector value.</summary>
        public Vector2? _vector = null;
        public Vector2 asVector { get { return (Vector2)_vector; } }

        /// <summary>Float value.</summary>
        public float _float = 0.0f;
        public float asFloat { get { return _float; } }
        public int asInt { get { return (int)_float; } }

        /// <summary>bool value.</summary>
        public bool _bool = false;
        public bool asBool { get { return _bool; } }
}

And I try to write an XML with a dictionary of these:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:ns="Microsoft.Xna.Framework">
  <Asset Type="System.Collections.Generic.Dictionary[System.String, StyleProperty]">
    <Item>
      <Key>Default.FillColor</Key>
      <Value>
        <StyleProperty xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <_color>
            <B>0</B>
            <G>0</G>
            <R>255</R>
            <A>255</A>
            <PackedValue>4278190335</PackedValue>
          </_color>
          <_vector xsi:nil="true" />
          <_float>0</_float>
          <_bool>false</_bool>
        </StyleProperty>
      </Value>
    </Item>
  </Asset>
</XnaContent>

However no matter what I put inside the “Value” tag (tried lots of things), I always get Value cannot be null error:

error : Importer 'XmlImporter' had unexpected failure!
1>  System.ArgumentNullException: Value cannot be null.
1>     at System.RuntimeType.MakeGenericType(Type[] instantiation)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.FindType(String typeName)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.Deserialize[T](XmlReader input, String referenceRelocationPath)
1>     at Microsoft.Xna.Framework.Content.Pipeline.XmlImporter.Import(String filename, ContentImporterContext context)
1>     at Microsoft.Xna.Framework.Content.Pipeline.ContentImporter`1.Microsoft.Xna.Framework.Content.Pipeline.IContentImporter.Import(String filename, ContentImporterContext context)
1>     at MonoGame.Framework.Content.Pipeline.Builder.PipelineManager.ProcessContent(PipelineBuildEvent pipelineEvent)

What am I doing wrong here?

Thanks! :slight_smile:

EDIT: I just noticed that this happens even if I remove the <Item> tag, eg the error comes just from the fact that I’m trying to define type="System.Collections.Generic.Dictionary[System.String, StyleProperty]".

EDIT2: and even if I use a simple object that only has one public int I’m still getting it. Is this a bug in MonoGame?

Replying to self - apparently my Content pipeline Reference was the release dll, and I was building the debug dll. In the release dll the StyleProperty didn’t exist (I would expect different error message but now it works).

However now I’m facing different problem - I’m not sure how to format the value element. Pretty much anything I try brings up the 'Element' is an invalid XmlNodeType error:

error : Importer 'XmlImporter' had unexpected failure!
1>  Microsoft.Xna.Framework.Content.Pipeline.InvalidContentException: An error occured parsing. ---> System.Xml.XmlException: 'Element' is an invalid XmlNodeType. Line 7, position 10.
1>     at System.Xml.XmlReader.ReadEndElement()
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.DictionarySerializer`2.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Dictionary`2 existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.ContentTypeSerializer`1.Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadRawObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateReader.ReadObject[T](ContentSerializerAttribute format)
1>     at Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate.IntermediateSerializer.Deserialize[T](XmlReader input, String referenceRelocationPath)
1>     --- End of inner exception stack trace ---

How can I understand the format expected from me as the dictionary ?

Thanks!

There are some known issues with the XML serialization. I don’t have an answer right now for this issue. It is an area that we will have to focus on. I can have a look tonight to see where this might be going wrong.

Cool thanks :slight_smile:

If there’s anything I can do to help let me know. Also, if you got any examples of a dictionary holding custom objects that I can take a look on it might help, I’m still not sure if its really a problem or I just did something wrong on my side…

Hello! Just wondering: Are you trying to store whole classes as XML, so your program can load functionality on the fly, rather than loading a huge class library on launch?

Or are you just trying to save/load values for various variables IN your classes…?

Or is this something else?

This creates an entire stand-alone object.

You mean it loads classes or sub-classes, and not “instances” of classes or sub-classes?

-I ask because I was looking for this exact kind of thing, so I didn’t have to load all code for my enemy sub-classes on launch.

-But I had to resort to saving only variables…

An object is an instance of a class. XML serialization in the content pipeline allows you to add an XML file to the content project, the content pipeline will process the XML into a binary form in a XNB file, and then it can be loaded through the ContentManager.

In this case, StyleProperty will be in a class assembly that is referenced by both the content pipeline and the game. This is because StyleProperty is not a type in the standard .NET framework or MonoGame (which the content pipeline already knows about). The XML file is processed into a XNB that is placed in the Content directory in the build output. For example, if the XML file is called StyleDictionary.xml, this will produce StyleDictionary.xnb in the output.

In the game, the object is loaded through the ContentManager

Dictionary<string, StyleProperty> styleDictionary;
...
styleDictionary = Content.Load<Dictionary<string, StyleProperty>>("StyleDictionary");

ok, thanks, that’s what I thought… Still, I guess it might be nicer than saving all the individual variable components of an object one by one… Which is what I do atm…