Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
    
Your deserializing code would be useful here. –  Tetrad Apr 17 '11 at 10:27
3  
I have no deserializing code. Maybe this is where my problem starts? –  eflles Apr 17 '11 at 10:28
3  
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). –  Andrew Russell Apr 17 '11 at 13:40

2 Answers 2

up vote 13 down vote accepted

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.

share|improve this answer
1  
+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. –  michael.bartnett Apr 18 '11 at 1:35

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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