无法反序列化XML字符串

时间:2017-10-17 10:15:21

标签: c# xml xml-deserialization

我有以下xml,我试图反序列化如下,但我收到错误:

  

有一个错误反映了类型' System.Collections.Generic.List`1 [MyApp.Models.Field]

以下是我的代码

public class FieldList
{
    [XmlArray("fields")]
    [XmlArrayItem("field")]
    List<Field> Fields { get; set; }
}

public class Field
{
    [XmlAttribute("type")]
    public string Type { get; set; }

    [XmlAttribute("required")]
    public bool Required { get; set; }

    [XmlAttribute("label")]
    public string Label { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlElement("option")]
    [JsonProperty("values")]
    public List<Option> Options { get; set; }
}

public class Option
{
    [XmlAttribute("label")]
    public string Label { get; set; }

    [XmlAttribute("value")]
    public string Value { get; set; }

    [XmlAttribute("selected")]
    public bool Selected { get; set; }

    /// <remarks/>
    [XmlIgnore()]
    public bool SelectedSpecified { get; set; }

    [XmlText]
    public string Text { get; set; }
}

var xml =  @"<?xml version=""1.0"" ?>
   <form-template>
    <fields>
        <field type=""select"" required=""true"" label=""Cars"" name=""cars"" >
            <option label=""Toyota"" value=""Toyota"" selected=""true"">Toyota</option>
            <option label=""Nisan"" value=""Nisan"" >Nisan</option>
        </field>
    </fields>
   </form-template>";


 var serializer = new XmlSerializer(typeof(FieldList), new XmlRootAttribute("form-template"));
 var stringReader = new StringReader(xml);
 var xmlFields = (FieldList)serializer.Deserialize(stringReader);

我做错了什么?

*更新*

根据以下评论改变

 public IEnumerable<Option> Options { get; set; }

  public List<Option> Options { get; set; }

修复错误但现在没有反序列化 - 变量xmlFields是空的???我是否需要从特定节点读取或者它是否无关紧要?

3 个答案:

答案 0 :(得分:3)

XmlSerializer无法序列化抽象属性,所以请将IEnumerable更改为List,如下所示,

public IEnumerable<Option> Options { get; set; }

public List<Option> Options { get; set; }

您的根元素与您拥有的XML结构不匹配。我编辑了如下的XML,

var xml = "<?xml version=\"1.0\" ?>" +
                    "  " +
                    "    <Fields>" +
                    "        <Field Type=\"select\" Required=\"true\" Label=\"Cars\" Name=\"cars\" >" +
                    "            <option label=\"Toyota\" lalue=\"Toyota\" selected=\"true\">Toyota</option>" +
                    "            <option label=\"Nisan\" value=\"Nisan\" >Nisan</option>" +
                    "        </Field>" +
                    "    </Fields>";

将代码反序列化为,

XmlSerializer serializer = new XmlSerializer(typeof(List<Field>), new XmlRootAttribute("Fields"));

    var stringReader = new StringReader(xml);
        List<Field> xmlFields = (List<Field>)serializer.Deserialize(stringReader);

现在它正在反序列化。请参阅输出,

enter image description here

答案 1 :(得分:2)

让我们来看看你的代码。

new XmlRootAttribute("form-template")映射到<form-template>节点。

public class Field映射到<field节点。

但没有任何地图映射到<fields>节点。

添加以下课程:

public class FieldList
{
    [XmlArray("fields")]
    [XmlArrayItem("field")]
    public List<Field> Fields { get; set; }
}

应该有效:

var serializer = new XmlSerializer(typeof(FieldList), new XmlRootAttribute("form-template"));
var stringReader = new StringReader(xml);
var xmlFields = (FieldList)serializer.Deserialize(stringReader);

此外,您应该向Option类添加属性:

[XmlText]
public string Text { get; set; }

更新

你可以摆脱FieldList课程。但是你必须手动跳过部分xml节点。

List<Field> xmlFields;
var serializer = new XmlSerializer(typeof(List<Field>), new XmlRootAttribute("fields"));

// You can read from a stream or from a StringReader instead of a file
using (var xmlReader = XmlReader.Create("test.xml"))
{
    // Skip <form-template> node
    xmlReader.ReadToFollowing("fields");

    xmlFields = (List<Field>)serializer.Deserialize(xmlReader);
}

为您的班级添加XmlType属性:

[XmlType("field")]
public class Field

答案 2 :(得分:1)

您需要正确生成要将xml序列化到的类。复制正确的XML,不要使用双引号&#34;双引号&#34;进入剪贴板,转到Visual Studio中解决方案中的任何cs文件。点击编辑 - &gt;选择性粘贴 - &gt;将XML粘贴为类。这将在cs文件中正确生成类以进行转换。然后,将这些更改为自动实现的属性,而不是使用私有变量来减少屏幕上的混乱。

使用下面的一组类,反序列化为

XmlSerializer serializer = new XmlSerializer(typeof(FormTemplate));
            var xml = @"<?xml version=""1.0"" ?>
   <form-template>
    <fields>
        <field type=""select"" required=""true"" label=""Cars"" name=""cars"" >
            <option label=""Toyota"" value=""Toyota"" selected=""true"">Toyota</option>
            <option label=""Nisan"" value=""Nisan"" >Nisan</option>
        </field>
    </fields>
   </form-template>";
            var stringReader = new StringReader(xml);
            var xmlFields = serializer.Deserialize(stringReader);

修改为自动生成的类集 将属性更改为自动实现的属性。

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("form-template", Namespace = "", IsNullable = false)]
public partial class FormTemplate
{
    /// <remarks/>
    public Fields fields { get; set; }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class Fields
{
    /// <remarks/>
    public SingleField field { get; set; }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class SingleField
{
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("option")]
    public Option[] option { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool required { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string label { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name { get; set; }
  }

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class Option
{
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string label { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string value { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool selected { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool selectedSpecified { get; set; }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value { get; set; }

}