你可以这样做:
1)首先你应该给类属性:
[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) 现在您可以像这样反序列化您的 XElement 或简单的 xml 字符串:
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);
我希望它对你有帮助。