2015-03-16 1 views
4

Je cherche à mapper plusieurs attributs XML dans des éléments imbriqués dans une seule classe POCO à l'aide de l'objet XmlSerializer.Mappage de XML aux classes dans C#

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

Ce serait génial si quelqu'un sait comment je peux faire ce.

Merci.

+0

Vous allez devoir mettre en œuvre 'IXmlSerializable' dans des produits et analyser manuellement le XML ou utilisez Linq pour XML. – SwDevMan81

Répondre

1

En supposant que le POCO est ce que vous pouvez modifier:

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

Si vous pouvez modifier le XML, vous pouvez le faire un peu plus facilement ne pas utiliser les attributs et juste coller aux éléments (au moins pour un, Deux et trois).

3

Si vous êtes enfermé dans ce schéma XML, cela sérialisation ou de désérialiser vos données XML et objet:

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