首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反序列化字符串列表

反序列化字符串列表
EN

Stack Overflow用户
提问于 2014-03-26 22:52:13
回答 3查看 515关注 0票数 1

我正在尝试将XML反序列化为C#对象。我尝试过许多方案,但无法在我的一生中得到反序列化,以获得choices。见下面的代码..。

XML..。

代码语言:javascript
复制
@"<?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>

我的c#模型..。

代码语言:javascript
复制
[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;
};

去序列化。

代码语言:javascript
复制
using System.Xml.Serialization;

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

结果显示为JSON..。

代码语言:javascript
复制
{"Questions":[
{"type":"multiple-choice","text":"Question 1","choices":[]},
{"type":"multiple-choice","text":"Question 2","choices":[]}
]}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-26 22:55:32

choices上添加这些属性

代码语言:javascript
复制
[XmlArray]
[XmlArrayItem("choice")]
public Choices choices;
票数 1
EN

Stack Overflow用户

发布于 2014-03-26 22:57:32

我可能超出了这个问题的范围,但是VS2013有一个非常酷的特性:Edit-->Paste Special--> Paste XML As Classes

代码语言:javascript
复制
/// <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;
    }
  }
}
票数 2
EN

Stack Overflow用户

发布于 2014-03-26 22:57:05

不是应该是public List<string> choices;而不是public Choices choices;吗?因为您对Question也做了同样的操作(使用了数组)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22674722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档