简体   繁体   中英

XML serialization in C#.Net, serialize all class properties

I wanna serialize this class:

[Serializable]
[XmlRoot(ElementName = "Rates")]
public class CbrRate : IRate
{
    public CbrRate()
    {
    }

    public CbrRate(DateTime date, ICurrency currency, decimal rate)
    {
        Currency = currency;
        Date = date;
        Rate = rate;
    }

    [XmlIgnore]
    public string SrcName
    {
        get { return "CBR"; }
    }

    [XmlElement(ElementName = "RequestDate")]
    public DateTime Date { get; set; }

    [XmlIgnore]
    public ICurrency Currency { get; set; }

    [XmlElement(ElementName = "Direction")]
    public string Direction
    {
        get { return "RUR=>" + CodeChar.Trim(); }
    }

    [XmlElement(ElementName = "RateValue")]
    public decimal Rate { get; set; }

    [XmlElement(ElementName = "RateBase")]
    public decimal BaseRate
    {
        get { return Math.Round(Rate/Nominal, 4); }
    }

    [XmlElement(ElementName = "RateCross")]
    public decimal CrossRate
    {
        get { return Math.Round(1.00M/BaseRate, 4); }
    }

    [XmlElement(ElementName = "CodeNum")]
    public int CodeNum
    {
        get { return Currency.CodeNumIso; }
    }

    [XmlElement(ElementName = "CodeISO")]
    public string CodeChar
    {
        get { return Currency.CodeCharIso; }
    }

    [XmlElement(ElementName = "CurrencyName")]
    public string Name
    {
        get { return Currency.Name; }
    }

    [XmlElement(ElementName = "Nominal")]
    public decimal Nominal
    {
        get { return Currency.Nominal; }
    }
}


public static XDocument Serialize<T>(this T source)
{
    var target = new XDocument();
    var s = new XmlSerializer(typeof (T));
    using (var writer = target.CreateWriter())
    {
        s.Serialize(writer, source);
        writer.Close();
    }
    return target;
}

But, that I have:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfCbrRate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CbrRate>
    <RequestDate>2011-09-05T18:49:55.1195696+04:00</RequestDate>
    <RateValue>31.0539</RateValue>
  </CbrRate>
...

How I can create correct xml, like this:

<ArrayOfRates>    
<Rates>
       <RequestDate></RequestDate>
       <Direction></Direction>
       <RateValue></RateValue>
       <RateBase></RateBase>
    ...

So, assuming you are serializing a List<CbrRate > , you will need to create a wrapper class for the list to have it serialized as you want it. Something like this:

[XmlRoot("root")]
public class ListOfRates 
{
    [XmlArray("ArrayOfRates")]
    [XmlArrayItem("Rate")]
    public List<CbrRate> Rates { get; set; }
}

this will produce the xml you want. Or you can play around with the attributes a little but if you don't want to have a root:

[XmlRoot("ArrayOfRates")]
public class ListOfRates 
{
    [XmlArrayItem("Rate")]
    public List<CbrRate> Rates { get; set; }
}

the two attributes XmlArray and XmlArrayItem are key here. If you don't provide a name for the xml element, it will default to the property name.

First of all, .Net XmlSerializer will only serialize read/write properties (or fields). That's why only RequestDate and RateValue are serialized.

In order to achieve the XML structure you mentioned, you need to create a wrapper class as Roel said.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM