小编Jay*_*Jay的帖子

将XML映射到c#中的类

我希望使用XmlSerializer对象将嵌套元素中的多个XML属性映射到单个POCO类中.

XML

<products grand-total="100">
    <one price="50" />
    <two price="20" />
    <tree price="30" />
</products>
Run Code Online (Sandbox Code Playgroud)

POCO

public class Product
{
    public int GrandTotal { get; set; }
    public int OnePrice { get; set; }
    public int TwoPrice { get; set; }
    public int ThreePrice { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

C#

var doc = XDocument.Load("XmlDoc.xml");
var serializer = new XmlSerializer(typeof(Product));
var reader = doc.Root.CreateReader();
var temp = (Product)serializer.Deserialize(reader);
Run Code Online (Sandbox Code Playgroud)

如果有人知道如何做到这一点,那将是非常棒的.

谢谢.

c# xml attributes poco deserialization

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

attributes ×1

c# ×1

deserialization ×1

poco ×1

xml ×1