使用列表将XML反序列化为C#对象

使用列表将XML反序列化为C#对象,c#,xml,deserialization,xml-deserialization,C#,Xml,Deserialization,Xml Deserialization,我试图将XML反序列化为一个C#对象,该对象具有许多相同类型的元素。为了清楚起见,我删减了内容。我的C#类看起来像这样: [XmlInclude(typeof(RootElement))] [XmlInclude(typeof(Entry))] [Serializable, XmlRoot("Form")] public class DeserializedClass { public List<Entry> listEntry; public RootElement

我试图将XML反序列化为一个C#对象,该对象具有许多相同类型的元素。为了清楚起见,我删减了内容。我的C#类看起来像这样:

[XmlInclude(typeof(RootElement))]
[XmlInclude(typeof(Entry))]
[Serializable, XmlRoot("Form")]
public class DeserializedClass
{
    public List<Entry> listEntry;
    public RootElement rootElement { get; set; }
}
<Entry property="value">
  <entryValue1>Data 1</entryValue1>
  <entryValue2>Data 2</entryValue2>
  <RootElement>
    <rootElementValue1>Data 3</rootElementValue1>
    <rootElementValue2>Data 4</rootElementValue2>
  </RootElement>
  <RootElement>
    <rootElementValue1>Data 5</rootElementValue1>
    <rootElementValue2>Data 6</rootElementValue2>
  </RootElement>
</Entry>
我试图反序列化的XML的结构如下所示:

[XmlInclude(typeof(RootElement))]
[XmlInclude(typeof(Entry))]
[Serializable, XmlRoot("Form")]
public class DeserializedClass
{
    public List<Entry> listEntry;
    public RootElement rootElement { get; set; }
}
<Entry property="value">
  <entryValue1>Data 1</entryValue1>
  <entryValue2>Data 2</entryValue2>
  <RootElement>
    <rootElementValue1>Data 3</rootElementValue1>
    <rootElementValue2>Data 4</rootElementValue2>
  </RootElement>
  <RootElement>
    <rootElementValue1>Data 5</rootElementValue1>
    <rootElementValue2>Data 6</rootElementValue2>
  </RootElement>
</Entry>

有没有办法解决这个问题?

为了让反序列化代码正常工作,我稍微调整了一下类:

[Serializable, XmlRoot("Entry")]
public class DeserializedClass
{
    public string entryValue1;
    public string entryValue2;

    [XmlElement("RootElement")]
    public List<RootElement> rootElement { get; set; }
}

public class RootElement
{
    public string rootElementValue1 { get; set; }
    public string rootElementValue2 { get; set; }
}
[Serializable,XmlRoot(“条目”)]
公共类反序列化类
{
公共字符串entryValue1;
公共字符串entryValue2;
[XmlElement(“RootElement”)]
公共列表根元素{get;set;}
}
公共类根元素
{
公共字符串rootElementValue1{get;set;}
公共字符串rootElementValue2{get;set;}
}
现在它工作得很好

我不知道为什么您将XmlRoot声明为“Form”,因为XML中没有具有该名称的元素,所以我将其替换为“Entry”


不能使用entryvalue1和entryvalue2属性的条目类,因为它们是根(事件)的直接子级,并且没有子级作为条目。简而言之,类必须反映XML的层次结构,以便反序列化能够正常工作

为了让反序列化代码正常工作,我稍微调整了一下类:

[Serializable, XmlRoot("Entry")]
public class DeserializedClass
{
    public string entryValue1;
    public string entryValue2;

    [XmlElement("RootElement")]
    public List<RootElement> rootElement { get; set; }
}

public class RootElement
{
    public string rootElementValue1 { get; set; }
    public string rootElementValue2 { get; set; }
}
[Serializable,XmlRoot(“条目”)]
公共类反序列化类
{
公共字符串entryValue1;
公共字符串entryValue2;
[XmlElement(“RootElement”)]
公共列表根元素{get;set;}
}
公共类根元素
{
公共字符串rootElementValue1{get;set;}
公共字符串rootElementValue2{get;set;}
}
现在它工作得很好

我不知道为什么您将XmlRoot声明为“Form”,因为XML中没有具有该名称的元素,所以我将其替换为“Entry”

不能使用entryvalue1和entryvalue2属性的条目类,因为它们是根(事件)的直接子级,并且没有子级作为条目。简而言之,类必须反映XML的层次结构,以便反序列化能够正常工作