spiderplant0
spiderplant0

Reputation: 3952

Deserialize a list of strings

I'm trying to deserialize XML into a C# object. I've tryed many schemes but cant for the life of me get the deserialisation to pick up the choices. See code below ...

The XML...

@"<?xml version=""1.0"" encoding=""UTF-8""?>
<survey>
    <question>
        <type>multiple-choice</type>
        <text>Question 1</text>
        <choices>
            <choice>Answer A</choice>
            <choice>Answer B</choice>
            <choice>Answer C</choice>
        </choices>
    </question>
    <question>
        <type>multiple-choice</type>
        <text>Question 2</text>
        <choices>
            <choice>Answer a</choice>
            <choice>Answer b</choice>
        </choices>
    </question>
</survey>

My c# model...

[XmlType("question")]
public struct Question
{
    public String type;
    public String text;
    public Choices choices;
};

public class Choices : List<String> { };

[XmlType("survey")]
public class Survey
{
    [XmlElement(ElementName = "question")]
    public Question[] Questions;
};

Deserialisation...

using System.Xml.Serialization;

Survey survey;
XmlSerializer serializer = new XmlSerializer(typeof(Survey));
survey = (Survey)serializer.Deserialize(reader);

The result shown as JSON...

{"Questions":[
{"type":"multiple-choice","text":"Question 1","choices":[]},
{"type":"multiple-choice","text":"Question 2","choices":[]}
]}

Upvotes: 1

Views: 531

Answers (3)

MikeH
MikeH

Reputation: 4395

I could be going too far beyond the scope of this question, but VS2013 has a really cool feature: Edit-->Paste Special--> Paste XML As Classes

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class survey
{

  private surveyQuestion[] questionField;

  /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute("question")]
  public surveyQuestion[] question
  {
    get
    {
      return this.questionField;
    }
    set
    {
      this.questionField = value;
    }
  }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class surveyQuestion
{

  private string typeField;

  private string textField;

  private string[] choicesField;

  /// <remarks/>
  public string type
  {
    get
    {
      return this.typeField;
    }
    set
    {
      this.typeField = value;
    }
  }

  /// <remarks/>
  public string text
  {
    get
    {
      return this.textField;
    }
    set
    {
      this.textField = value;
    }
  }

  /// <remarks/>
  [System.Xml.Serialization.XmlArrayItemAttribute("choice", IsNullable = false)]
  public string[] choices
  {
    get
    {
      return this.choicesField;
    }
    set
    {
      this.choicesField = value;
    }
  }
}

Upvotes: 2

Markus Safar
Markus Safar

Reputation: 6580

Shouldn't it be public List<string> choices; instead of public Choices choices;? Because you did the same with Question (used an array).

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292555

Add these attributes on choices:

[XmlArray]
[XmlArrayItem("choice")]
public Choices choices;

Upvotes: 1

Related Questions