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" ?>
<Asset Type="System.Collections.Generic.List[System.String]">


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


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

  • \$\begingroup\$ I don't think that XML structure fits the List<string> type. The XmlSerializer is probably having trouble with that. \$\endgroup\$ – Michael Coleman Apr 19 '11 at 14:03

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

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"?>
<Asset Type="MyNamespace.Question">
    <question>What is 3 x 3? Orange = 9, Pear = 10, Apple = 12</question>
  • \$\begingroup\$ 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. \$\endgroup\$ – Tom Apr 19 '11 at 16:28
  • \$\begingroup\$ Also, your example above is for one question. My game will have multiple questions. How would that be implemented? \$\endgroup\$ – Tom Apr 19 '11 at 16:50
  • 1
    \$\begingroup\$ Use a list of questions. The Type would be "System.Collections.Generic.List[YourNamespace.Question]" \$\endgroup\$ – Michael Coleman Apr 19 '11 at 18:04
  • \$\begingroup\$ @Tom: What Omnion said. Remember to use IntermediateSerializer.Serialize to check these things. \$\endgroup\$ – Andrew Russell Apr 20 '11 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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