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've got a snake game with three objects: apple, pear & orange. I would like these to be associated with questions which I'm trying to create in XML. This is what I've done so far...

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="System.Collections.Generic.List[System.String]">

<MathsQuiz>

<Question Value ="What is 3 x 3? Orange = 9, Pear = 10, Apple = 12" Right ="orange">
    <answer Value ="orange"/>
    <answer Value ="pear"/>
    <answer Value ="apple"/>
  </Question>

</MathsQuiz>
</Asset> 
</XnaContent>

I'm getting the error: "Error There was an error while deserializing intermediate XML. 'Element' is an invalid XmlNodeType. Line 7, position 6."

I'm guessing it's because of the line MathsQuiz but not sure.

Any help is much appreciated!

Thanks, Tom

share|improve this question
    
I don't think that XML structure fits the List<string> type. The XmlSerializer is probably having trouble with that. –  Michael Coleman Apr 19 '11 at 14:03

1 Answer 1

Much like this question, you cannot simply make up the XML format!

You have to use a format which matches the objects you will be deserializing!

Just as in my answer to that question, you can simply create some of your objects, serialize them, and see what they should look like in XML such that they can be deserialized. For example:

var sb = new StringBuilder();
using(XmlWriter writer = XmlWriter.Create(sb))
{
    IntermediateSerializer.Serialize(writer, theRootObjectYouAreSerializing, null);
}
Console.Write(sb.ToString());

So in your case, if your question structure looks like this:

struct Question
{
    public string question; 
    public string correct;
    public List<string> answers;
}

Then your XML will look something like this:

<?xml version="1.0" encoding="utf-8"?>
<XnaContent>
<Asset Type="MyNamespace.Question">
    <question>What is 3 x 3? Orange = 9, Pear = 10, Apple = 12</question>
    <correct>orange</correct>
    <answers>
        <Item>orange</Item>
        <Item>pear</Item>
        <Item>apple</Item>
    </answers>
</Asset>
</XnaContent>
share|improve this answer
    
Thanks for the reply. I'm a real beginner in terms of programming. I knew the XML document had to have a certain layout, I've missed some of the coding you have put. I tried to make it so that text was output just as a test, which worked when making just "<Items>" but wouldnt work the extra elements. Would I need a class for read and write to complete this XML? Thanks again. –  Tom Apr 19 '11 at 16:28
    
Also, your example above is for one question. My game will have multiple questions. How would that be implemented? –  Tom Apr 19 '11 at 16:50
1  
Use a list of questions. The Type would be "System.Collections.Generic.List[YourNamespace.Question]" –  Michael Coleman Apr 19 '11 at 18:04
    
@Tom: What Omnion said. Remember to use IntermediateSerializer.Serialize to check these things. –  Andrew Russell Apr 20 '11 at 4:25

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.