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?
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; }
}
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.
Have you tried:
XElement element = //Your XElement
var serializer = new XmlSerializer(typeof(ProductAttribute));
(ProductAttribute)serializer.Deserialize(element.CreateReader())
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.
This seems fairly easy (at least without error checking...):
var res = new ProdicAttribute {
Flag = (string)element.Attribute("flag"),
Sect = (string)element.Attribute("sect"),
...
}
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
.