Numm3n
Numm3n

Reputation: 1031

Convert single XElement to object

I have a single XElement looking like this:

<row flag="1" sect="" header="" body="" extrainfo="0" />

Then I have a class looking like this:

public class ProductAttribute
{
    public string Flag { get; set; }
    public string Sect { get; set; }
    public string Header { get; set; }
    public string Body { get; set; }
    public string Extrainfo { get; set; }
}

How can I convert this XElement into a ProductAttribute object?

Upvotes: 18

Views: 28994

Answers (6)

Vladimir Kniazkov
Vladimir Kniazkov

Reputation: 309

You could do this way:

1) At first you should give attributes to the class:

[XmlRoot("row")]
public class ProductAttribute
{
    [XmlAttribute("flag")]
    public string Flag { get; set; }
    [XmlAttribute("sect")]
    public string Sect { get; set; }
    [XmlAttribute("header")]
    public string Header { get; set; }
    [XmlAttribute("body")]
    public string Body { get; set; }
    [XmlAttribute("extrainfo")]
    public string Extrainfo { get; set; }
}

2) Now you can deserialize your XElement or simple xml string like this:

ProductAttribute productAttribute = new ProductAttribute();
XElement xElement = XElement.Parse(
"<row flag='1' sect='3' header='4444' body='3434' extrainfo='0' />");

//StringReader reader = new StringReader(
//"<row flag='1' sect='3' header='4444' body='3434' extrainfo='0' />");

StringReader reader = new StringReader(xElement.ToString());
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ProductAttribute));
productAttribute = (ProductAttribute)xmlSerializer.Deserialize(reader);

I hope it helps you.

Upvotes: 14

jbl
jbl

Reputation: 15413

You have to put the correct serialization attributes on your class and class members

[Serializable()]
[XmlRoot(ElementName = "row")]
public class ProductAttribute
{
    [XmlAttribute("flag")]
    public string Flag { get; set; }
    [XmlAttribute("sect")]
    public string Sect { get; set; }
    [XmlAttribute("header")]
    public string Header { get; set; }
    [XmlAttribute("body")]
    public string Body { get; set; }
    [XmlAttribute("extrainfo")]
    public string Extrainfo { get; set; }
}

Upvotes: 19

Rob
Rob

Reputation: 1081

I would add a constructor that takes in a XElement.

public class ProductAttribute
{
    public string Flag { get; set; }
    public string Sect { get; set; }
    public string Header { get; set; }
    public string Body { get; set; }
    public string Extrainfo { get; set; }

    public ProductAttribute(XElement xElement){
        Flag = (string)element.Attribute("flag");
        Sect = (string)element.Attribute("sect").Value;
        Header = (string)element.Attribute("header ").Value;
        Body = (string)element.Attribute("body").Value;
        Extrainfo = (string)element.Attribute("extrainfo").Value;
    }
}

Then you can just call

var productAttribute = new ProductAttribute(element);

If you wanted to make that dynamic you could use reflection get the properties on the class then loop over then and searching the XElement for that attribute then setting that property much in the same way. However I would keep it simple as the object is not complex.

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

var element = XElement.Parse("<row flag="1" sect="" header="" body="" extrainfo="0" />");

var productAttribute = new ProductAttribute {
    Flag = (string)element.Attribute("flag"),
    Sect = (string)element.Attribute("sect"),
    Header = (string)element.Attribute("header"),
    Body = (string)element.Attribute("body"),
    Extrainfo = (string)element.Attribute("extrainfo")
};

But I don't think all ProductAttribute class properties should be typed as string.

Upvotes: 0

Richard
Richard

Reputation: 109140

This seems fairly easy (at least without error checking...):

var res = new ProdicAttribute {
  Flag = (string)element.Attribute("flag"),
  Sect = (string)element.Attribute("sect"),
  ...
}

Upvotes: 2

Alberto
Alberto

Reputation: 15951

Have you tried:

XElement element = //Your XElement
var serializer = new XmlSerializer(typeof(ProductAttribute));
(ProductAttribute)serializer.Deserialize(element.CreateReader()) 

Upvotes: 10

Related Questions