开发者

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

开发者 https://www.devze.com 2023-04-02 07:31 出处:网络
I wanna serialize this class: [Serializable] [XmlRoot(ElementName = \"Rates\")] public class CbrRate : IRate

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(Rat开发者_JAVA技巧e/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>
    ...


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消