Welcome to MSDN Blogs Sign in | Join | Help

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"?>
    <XnaContent>
      <Asset Type="XmlTest.MyTest">
        <elf>23</elf>
        <hello>Hello World</hello>
      </Asset>
    </XnaContent>

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

Published Tuesday, August 12, 2008 12:44 PM by ShawnHargreaves

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Wednesday, August 13, 2008 12:13 PM by dna8088

# re: Teaching a man to fish

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 :-)

Wednesday, August 13, 2008 12:16 PM by ShawnHargreaves

# re: Teaching a man to fish

dna8088: have you looked at this sample?

http://creators.xna.com/en-us/sample/winforms_series2

Thursday, August 14, 2008 10:28 AM by dna8088

# re: Teaching a man to fish

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 :-(

Monday, June 22, 2009 4:31 PM by Justin

# re: Teaching a man to fish

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

Thursday, October 01, 2009 10:40 PM by jk

# re: Teaching a man to fish

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).

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker