我有一个看起来像这样的 XElement
:
<row flag="1" sect="" header="" body="" extrainfo="0" />
然后我有一个看起来像这样的类:
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; }
}
如何将此 XElement
转换为 ProductAttribute
对象?
最佳答案
你必须在你的类和类成员上放置正确的序列化属性
[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; }
}
关于c# - 将单个 XElement 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608208/