Teaching a man to fish

Variants of the following question come up somewhat regularly on the XNA forums:

  • I want to import game data from an XML file
  • I plan to do this using the Content Pipeline IntermediateSerializer
  • I have my custom data type all ready to go
  • But I don't know how I should format the XML for it!

You already have the tools to answer this yourself. Here's how:

Fire up Visual Studio and create a new Console Application project.

Right-click on the References node, and add the Microsoft.Xna.Framework.Content.Pipeline assembly.

Add using declarations for the System.Xml and Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate namespaces.

Add a test class with the same layout as whatever data you want to serialize, but initialized with dummy test values. For instance:

    namespace XmlTest
        class MyTest
            public int elf = 23;
            public string hello = "Hello World";

Add this code to Main:

    MyTest testData = new MyTest();

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;

    using (XmlWriter writer = XmlWriter.Create("test.xml", settings))
        IntermediateSerializer.Serialize(writer, testData, null);

Run the program. Look in the bin\Debug folder, and open the test.xml output file. With the class shown above, this will look like:

    <?xml version="1.0" encoding="utf-8"?>
      <Asset Type="XmlTest.MyTest">
        <hello>Hello World</hello>

Tada! That's how IntermediateSerializer represents this particular class in XML.

2011 update: as of XNA 4.0 you must change your project to target the full .NET Framework, as opposed to the Client Profile, before adding the Content Pipeline assembly reference. Nick has details.

Comments (16)

  1. dna8088 says:

    I wanted to be able to build my spritesheets in my editor to do this I need a way to compile the spritesheet from a WinForms App into an .xnb file…

    Any pointers would be good 🙂

  2. ShawnHargreaves says:

    dna8088: have you looked at this sample?


  3. dna8088 says:

    Thanks for the link, But…

    "Note also that the XNA Framework redistributable installer does not include the Content Pipeline."

    That’s a bit of a showstopper if I wanted to distribute my editor with image compiler feature 🙁

  4. Justin says:

    That seems kinda odd, writing a class first with all the info you need in your app to convert it into xml and open the xml again

  5. Aaron says:

    I would imagine that the point is you can load your class in-game then fill it with the neccessary data and serialise it after its all updated…

  6. jk says:

    Justin: I think this was an exercise to get an idea of how the XML should be formed and not an example of how to build an app to write out the XML.  Granted, you could do it just like this and just have your editor instantiate all of the objects before writing them out, but you can just as easily have your editor (or game designer) write out XML using a different method (such as Notepad).

  7. Andrew Sadler says:

    I’m currently trying to do the exact opposite, and use my xml spriteSheets from the XNA sample with the Winforms content loader example to build a map editor.  I can’t   get the program to recognise the custom processor.  




    "Microsoft.Xna.Framework.Content.Pipeline.SpriteSheetProcessor" + xnaVersion

    I’ve added the references but can’t seem to make it work and can’t find the any answer on the forums.

    Is there an example of combining the Winforms Content Loader example with the spritesheets example?  Is it possible?

  8. Essay says:

    Actually, I am trying to understand now the relevance of these codes and how to apply them on my own. I am a fresh computer science student and I am currently learning the basic programming languages like C. Also, our teacher wants us to give her an essay explaining the internal processes going on behind the programming language.

  9. As a profession i am a dentist but i have a lot interest in web programming so i am start learning coding and other language to make my site more competent.

  10. Hello! I would imagine that the point is you can load your class in-game then fill it with the necessary data and serialize it after its all updated.

  11. Michael says:

    For this to work, the full ".NET Framework 4.0" as target framework is needed. When using ".NET Framework 4.0 Client Profile" the code can't be compiled.

  12. aztectrev says:

    exactly what i was looking for! been struggling at making my data from scratch on my my own. cheers

  13. Blah says:

    This info is available in a million places and everyone who has an error "at all" seems to get pushed to one of the many blogs with this information. I have yet to see someones issue actually get solved by one of these blog posts…sorry to vent here, just sick of reading this exact same info on 20 different blogs…

  14. ShawnHargreaves says:

    Hi Blah,

    I can assure you the technique described in this article absolutely does work to discover what format XML should be used to deserialize into any custom type using IntermediateSerializer.

    If your problem is something other than that, obviously this article will not help you. Afraid there's not much I can do to help you there, since you don't say what problem you are having!

  15. AllAssignmentHelp says:

    I didnt understand anything from this code, seems broken

  16. MakeMyAssignments says:

    Ofcourse the code seems broken. Even I cant understand everything.

Skip to main content