C# - XmlSerializer派生类

时间:2017-08-15 19:44:29

标签: c# list xmlserializer derived-class

我遇到了一个让我疯狂的问题,我在所有地方看都没有成功。 我试图使用.Net XmlSerializer序列化一些类。 我不想列出每个对象(列出[creditRating]和列表) [价钱])。我使用了两个对象作为我的问题,但我还有更多。

我想要达到的输出:

<?xml version="1.0" encoding="utf-8"?>
<workspace xmlns="myNamespace">      
  <marketData date="2017-08-10">
   <creditRatings>                         <!-- those are the tags I cant make :( -->
    <creditRating id="TestAsset" rating="BBB" />
   </creditRatings>                        <!-- those are the tags I cant make :( -->
   <prices>                                <!-- those are the tags I cant make :( -->
     <price id="TestAsset" currency="CHF" price="100" />
     <price id="TestAsset2" currency="CHF" price="100" />
    <prices>                               <!-- those are the tags I cant make :( -->
  </marketData>
  <marketData date="2017-08-11">
    <creditRatings>                        <!-- those are the tags I cant make :( -->
     <creditRating id="TestAsset" rating="B" />
     <creditRating id="TestAsset2" rating="AAA" />
    </creditRatings>                        <!-- those are the tags I cant make :( -->
  </marketData>
</workspace>

正如你所看到的,我设法创建了除了#34; creditRating&#34;之外的所有内容。和&#34;价格&#34;。我试着摆弄[XmlInclude]和[XmlArrayItem]属性但没有成功。

我想创建一个抽象基类列表,这样我就可以轻松填充它们,然后使用xmlSerializer来完成剩下的工作。

这是我的全部代码。

class Program
    {
        [XmlRoot("workspace")]
        public class Workspace
        {
            [XmlElement("marketData")]
            public List<MarketData> MarkeDatas { get; set; } = new List<MarketData>();
        }
        public class MarketData
        {
            [XmlAttribute(AttributeName = "date", DataType = "date")]
            public DateTime Date { get; set; }

            [XmlElement("creditRating", typeof(CreditRatingsMarketData))]
            [XmlElement("price", typeof(PricesMarketData))]
            public List<MarketDataItem> MarketDataItems { get; set; }
        }
        public abstract class MarketDataItem
        {
            [XmlIgnore]
            public DateTime Date { get; set; }
            [XmlAttribute("id")]
            public string Id { get; set; }

        }
        public class CreditRatingsMarketData : MarketDataItem
        {
            [XmlAttribute("rating")]
            public string CreditRating { get; set; }
        }

        public class PricesMarketData : MarketDataItem
        {
            [XmlAttribute("price")]
            public double Price { get; set; }
        }
        private static void Main(string[] args)
        {
            var listMd = new List<MarketDataItem>
            {
                new CreditRatingsMarketData {Date = DateTime.Today.AddDays(-5), CreditRating = "BBB", Id = "TestAsset"},
                new CreditRatingsMarketData {Date = DateTime.Today.AddDays(-4), CreditRating = "B", Id = "TestAsset"},
                new PricesMarketData {Date = DateTime.Today.AddDays(-5), Id = "TestAsset", Price = 100}
            };
            var mds = listMd.GroupBy(c => c.Date)
                .Select(c => new MarketData {Date = c.Key, MarketDataItems = c.ToList()})
                .ToList();

            var ws = new Workspace { MarkeDatas = mds };

            var serializer = new XmlSerializer(typeof(Workspace));
            var writer = new StreamWriter(@"C:\Test.xml");
            serializer.Serialize(writer, ws);
        }
    }

谢谢你的时间!

0 个答案:

没有答案