I'm very new to XML serialization, up until now I've been using XDocument and iterating through an XML file to load stuff up. I know there's a way to serialize XML files, recently I came across the following bit of XML:
<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="System.Collections.Generic.Dictionary[System.String, Microsoft.Xna.Framework.Rectangle]">
<Item><Key>frame0</Key><Value>0 0 128 128</Value></Item>
<Item><Key>frame1</Key><Value>128 0 128 128</Value></Item>
</Asset>
</XnaContent>
And have been trying to figure out how I'd load it up and parse it into a dictionary, the top part stating asset type=System.collections.generic.dictionary seems to suggest I should be able to quickly convert this into a dictionary.
So my question is how would I do that? I could easily iterate through the xml structure and match up keys to values but it looks like there would be a better way to do it?
EDIT: Having just skimmed this article and this one, I suspect you want to use either
IntermediateSerializer
orXmlImporter
.Not being an XNA developer, it's unclear to me how it all fits together, but hopefully that's enough to get you started.
Of course, if this was just an example of XML and you're actually wanting to serialize data that isn't part of XNA, you may want to look at
XmlSerializer
. Personally I've found it simpler to control the serialization manually using LINQ to XML - it's easier to manage different versions etc that way - but in some casesXmlSerializer
would be more convenient.It's surprisingly straight forward. Add the XML file to your content project. Keep the default importer ("XML Content") and processor (none).
The XNA content pipeline will build it into an
XNB
file for you.Then simply load it with
ContentManager.Load
like you would any other asset: