Mapping XML to classes in c#

僤鯓⒐⒋嵵緔 提交于 2021-02-18 23:30:51

问题


I'm looking to map multiple XML attributes in nested elements into a single POCO class using the XmlSerializer object.

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

It would be awesome if anyone knows how I can do this.

Thanks.


回答1:


If you're locked into this XML schema, this will serialize or de-serialize your XML and object data:

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:


Assuming the POCO is what you can modify:

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

If you can modify the XML, you can do it a little more easily by not using attributes and just sticking to elements (at least for One, Two, and Three).



来源:https://stackoverflow.com/questions/29085381/mapping-xml-to-classes-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!