Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将带有主体和属性的标记反序列化到对象_C#_.net_Xml Serialization_Xml Deserialization - Fatal编程技术网

C# 将带有主体和属性的标记反序列化到对象

C# 将带有主体和属性的标记反序列化到对象,c#,.net,xml-serialization,xml-deserialization,C#,.net,Xml Serialization,Xml Deserialization,如何将这样的XML反序列化为对象: <Root> <Element Attr="AttrValue1">BodyValue1</Element> <Element Attr="AttrValue2">BodyValue2</Element> <Element Attr="AttrValue3">BodyValue3</Element> </Root> BodyValue1 车身价

如何将这样的XML反序列化为对象:

<Root>
   <Element Attr="AttrValue1">BodyValue1</Element>
   <Element Attr="AttrValue2">BodyValue2</Element>
   <Element Attr="AttrValue3">BodyValue3</Element>
</Root>

BodyValue1
车身价值2
车身价值3
我需要具有适当属性的精确对象结构

我试过:

[XmlRoot("Root")]
public class EventFieldsRoot
{
    [XmlElement("Element")]
    public List<Element> Elements{ get; set; }
}

public class Element
{
    [XmlAttribute]
    public string Attr { get; set; }

    [XmlElement("")]
    public string Body { get; set; }
}
[XmlRoot(“根”)]
公共类EventFieldsRoot
{
[XmlElement(“元素”)]
公共列表元素{get;set;}
}
公共类元素
{
[XmlAttribute]
公共字符串Attr{get;set;}
[XmlElement(“”)
公共字符串体{get;set;}
}
该属性反序列化为good,但body为空。我怎样才能反序列化body呢?

很简单

public class Element
{
    [XmlAttribute]
    public string Attr { get; set; }

    [XmlText]
    public string Body { get; set; }
}
XmlText属性工作得很好。

简单

public class Element
{
    [XmlAttribute]
    public string Attr { get; set; }

    [XmlText]
    public string Body { get; set; }
}
XmlText属性工作正常