9
\$\begingroup\$

I have an XML file representing different car manufactures that will be available in my game. The file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="List[string]">
    <car>Audi</car>
    <car>BMW</car>
    <car>Nissan</car>
    <car>Volvo</car>
  </Asset>
</XnaContent>

When adding it into my content folder, the compiler return this error:

There was an error while deserializing intermediate XML. Cannot find type "List`1"

How can I create a list of strings, put it into XML and read it from XNA?

\$\endgroup\$
3
  • \$\begingroup\$ Your deserializing code would be useful here. \$\endgroup\$
    – Tetrad
    Apr 17, 2011 at 10:27
  • 3
    \$\begingroup\$ I have no deserializing code. Maybe this is where my problem starts? \$\endgroup\$
    – eflles
    Apr 17, 2011 at 10:28
  • 3
    \$\begingroup\$ To answer Tetrad's question: presumably eflles is using the XmlImporter, part of the XNA Content Pipeline. The pipeline deserializes the XML in order to convert it to an XNB file (which will then be loaded by ContentManager at runtime). \$\endgroup\$ Apr 17, 2011 at 13:40

2 Answers 2

14
\$\begingroup\$

I haven't tried to find out exactly what is wrong with your XML - it's probably the lack of a namespace. But here is the correct XML:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent xmlns:Generic="System.Collections.Generic">
  <Asset Type="Generic:List[string]">
    <Item>Audi</Item>
    <Item>BMW</Item>
    <Item>Nissan</Item>
    <Item>Volvo</Item>
  </Asset>
</XnaContent>

Under the hood the XmlImporter uses the IntermediateSerializer class. So a small program like the following can be used to determine what your XML should look like:

var test = new List<string>(){ "Audi", "BMW", "Nissan", "Volvo" };
var sb = new StringBuilder();
using(XmlWriter writer = XmlWriter.Create(sb))
{
    IntermediateSerializer.Serialize(writer, test, null);
}
Console.Write(sb.ToString());

(You can even convert the output to a stream, pass it to the Deserialize method, and reconstruct the object - just to be doubly-sure it's working. Also it is probably worth putting the right encoding in the header - eg: StringBuilder is utf-16.)

For some in-depth information about IntermediateSerializer, take a look at Shawn Hargreaves' blog, starting with this post and continuing through the archives until August 2008.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ +1 Shawn Hargreaves' series of posts about this is great. Easiest way to generate your XML is to have a "LevelEditor" project that has all the same code, but references the Content Pipeline assemblies so you can create your objects with defaults attributes in code, and then serialize them to XML for loading in your actual game project. \$\endgroup\$ Apr 18, 2011 at 1:35
0
\$\begingroup\$

If you want to see how to do it by code example, here is a sample that uses xml like you want to http://create.msdn.com/en-US/education/catalog/sample/collision

If you want to read articles explaining whats going on and gain insight on how to work with xml & the content processor, go to this link, scroll down to the intermediate serializer section and read the 8 or 9 articles there. http://www.talula.demon.co.uk/blogindex.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .