将XML映射到C#类

8

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

XML

<products grand-total="100">
    <one price="50" />
    <two price="20" />
    <tree price="30" />
</products>

POCO

public class Product
{
    public int GrandTotal { get; set; }
    public int OnePrice { get; set; }
    public int TwoPrice { get; set; }
    public int ThreePrice { get; set; }
}

C#

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

如果有人知道我该如何做到这一点,那将是太棒了。

谢谢。


你需要在Product类中实现IXmlSerializable接口并手动解析XML,或者使用Linq to XML。 - SwDevMan81
2个回答

6
如果您已经使用了这个XML架构,那么这个方法能够将您的XML和对象数据进行序列化或反序列化:
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class ProductsViewModel
{
    public string Xml { get; set; }

    public Product Poco { get; set; }

    public ProductsViewModel()
    {
        Xml = Serialize(new Product());

        Poco = (Product)Deserialize(Xml, typeof(Product));
    }

    public class Price
    {
        [XmlAttribute(AttributeName = "price")]
        public int Value { get; set; }
    }

    [XmlRoot(ElementName = "products")]
    public class Product
    {
        [XmlAttribute(AttributeName = "grand-total")]
        public int GrandTotal { get; set; }

        [XmlElement(ElementName = "one")]
        public Price OnePrice { get; set; }

        [XmlElement(ElementName = "two")]
        public Price TwoPrice { get; set; }

        [XmlElement(ElementName = "tree")]
        public Price ThreePrice { get; set; }

        public Product()
        {
            GrandTotal = 100;
            OnePrice = new Price { Value = 50 };
            TwoPrice = new Price { Value = 20 };
            ThreePrice = new Price { Value = 30 };
        }
    }

    private string Serialize(object obj)
    {
        var serializer = new XmlSerializer(obj.GetType());

        using (var stringWriter = new StringWriter())
        {
            serializer.Serialize(stringWriter, obj);
            return stringWriter.ToString();
        }
    }

    private object Deserialize(string serializedObj, Type type)
    {
        var serializer = new XmlSerializer(type);

        using (var stringReader = new StringReader(serializedObj))
        using (var xmlTextReader = new XmlTextReader(stringReader))
        {
            return serializer.Deserialize(xmlTextReader);
        }
    }
}

2
假设您可以修改POCO:
假设您可以修改POCO:
[Serializable]
[XmlRoot("one")]
public class OnePrice
{
    [XmlAttribute("price")]
    public int price { get; set; }
}

[Serializable]
[XmlRoot("two")]
public class TwoPrice
{
    [XmlAttribute("price")]
    public int price { get; set; }
}

[Serializable]
[XmlRoot("three")]
public class ThreePrice
{
    [XmlAttribute("price")]
    public int price { get; set; }
}


[Serializable]
[XmlRoot("products")]    
public class Product
{        
    [XmlAttribute("grand-total")]
    public int GrandTotal { get; set; }

    [XmlElement("one")]
    public OnePrice OnePrice { get; set; }

    [XmlElement("two")]
    public TwoPrice TwoPrice { get; set; }

    [XmlElement("three")]
    public ThreePrice ThreePrice { get; set; }
}

如果您可以修改XML,那么您可以更轻松地完成它,只需使用元素而不是属性(至少对于One,Two和Three)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接