c#使用嵌套类反序列化自定义类

c#使用嵌套类反序列化自定义类,c#,serialization,xml-serialization,deserialization,xml-deserialization,C#,Serialization,Xml Serialization,Deserialization,Xml Deserialization,我正在尝试将xml反序列化为自定义对象,我很接近,但是嵌套最多的元素没有填充,而所有父元素都正常工作 以下是用于反序列化过程的xml和类: <?xml version="1.0" encoding="utf-8" ?> <dataMapping> <documentType attr1="blah" attr2="blah2" attr3="blah3"> <indexFields> <indexField name1

我正在尝试将xml反序列化为自定义对象,我很接近,但是嵌套最多的元素没有填充,而所有父元素都正常工作

以下是用于反序列化过程的xml和类:

<?xml version="1.0" encoding="utf-8" ?>
<dataMapping>
  <documentType attr1="blah" attr2="blah2" attr3="blah3">
    <indexFields>
      <indexField name1="field1A" name2="field1B" type="int" />
      <indexField name1="field2A" name2="field2B" type="int" />
      <indexField name1="field3A" name2="field3B" type="int" />
    </indexFields>
  </documentType>
  <documentType attr1="asdf" attr2="asdf2" attr3="asdf3">
    <indexFields>
      <indexField name1="field1A" name2="field1B" type="int" />
      <indexField name1="field2A" name2="field2B" type="int" />
      <indexField name1="field3A" name2="field3B" type="int" />
    </indexFields>
  </documentType>
</dataMapping>


[XmlRoot("dataMapping")]
public class dataMapping
{
    [XmlElement("documentType")]
    public List<DocumentType> DocumentTypes{ get; set; }

    public dataMapping() { }
}

[XmlRoot("documentType")]
public class DocumentType
{
    [XmlAttribute("attr1")]
    public string Area { get; set; }

    [XmlAttribute("attr2")]
    public string Cabinet { get; set; }

    [XmlAttribute("attr3")]
    public string SearchGroup { get; set; }

    [XmlElement("indexFields")]
    public List<IndexField> IndexFields{ get; set; }

    public DocumentType() { }
}

[XmlRoot("indexField")]
public class IndexField
{
    [XmlAttribute("name1")]
    public string Name1 { get; set; }

    [XmlAttribute("name2")]
    public string Name2 { get; set; }

    [XmlAttribute("type")]
    public string DataType { get; set; }

    public string ObjectValue { get; set; }

    public IndexField() { }
}

[XmlRoot(“数据映射”)]
公共类数据映射
{
[XmlElement(“documentType”)]
公共列表文档类型{get;set;}
公共数据映射(){}
}
[XmlRoot(“文档类型”)]
公共类文档类型
{
[XmlAttribute(“attr1”)]
公共字符串区域{get;set;}
[xmldattribute(“attr2”)]
公共字符串文件柜{get;set;}
[XmlAttribute(“attr3”)]
公共字符串搜索组{get;set;}
[XmlElement(“indexFields”)]
公共列表索引字段{get;set;}
公共文档类型(){}
}
[XmlRoot(“indexField”)]
公共类索引字段
{
[XmlAttribute(“名称1”)]
公共字符串名称1{get;set;}
[XmlAttribute(“名称2”)]
公共字符串名称2{get;set;}
[XmlAttribute(“类型”)]
公共字符串数据类型{get;set;}
公共字符串ObjectValue{get;set;}
公共索引字段(){}
}
因此,当通过反序列化创建自定义对象时,除了索引字段及其关联属性之外,所有内容都在填充。我在设置该类时哪里错了?

尝试添加它应该会有所帮助

[XmlArrayItem(typeof(IndexField))]
public List<IndexField> IndexFields{ get; set; }
[XmlArrayItem(typeof(IndexField))]
公共列表索引字段{get;set;}

把它弄明白了。我还需要一个名为“IndexFields”的类(基于xml的结构化方式),然后它包含一个“IndexField”列表……只是缺少一个级别

[XmlRoot("indexFields")]
public class IndexFields
{
    [XmlElement("indexField")]
    public List<IndexField> NestedIndexFields { get; set; }

    public IndexFields() { }
}
[XmlRoot(“indexFields”)]
公共类索引字段
{
[xmlement(“indexField”)]
公共列表嵌套索引字段{get;set;}
公共索引字段(){}
}