Convert single XElement to object

2020-08-09 09:18发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

Have you tried:

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


回答4:

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.



回答5:

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

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


回答6:

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.