Pregunta

How can I deserialize XML like that to an object:

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

I need the exact objects structure with appropriate attributes.

I've tried:

[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; }
}

The attribute deserializes good but body is empty. How can I deserialize body as well?

¿Fue útil?

Solución

Simply

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

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

XmlText attribute worked out perfectly.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top