Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 373
Numm3n Avatar asked Sep 04 '13 07:09

Numm3n


4 Answers

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; }
}
like image 57
jbl Avatar answered Nov 10 '22 21:11

jbl


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.

like image 42
Vladimir Kniazkov Avatar answered Nov 10 '22 20:11

Vladimir Kniazkov


Have you tried:

XElement element = //Your XElement
var serializer = new XmlSerializer(typeof(ProductAttribute));
(ProductAttribute)serializer.Deserialize(element.CreateReader()) 
like image 8
Alberto Avatar answered Nov 10 '22 22:11

Alberto


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.

like image 3
Rob Avatar answered Nov 10 '22 22:11

Rob