【问题标题】:How to de-serialise a list with multiple xsi:type如何反序列化具有多个 xsi:type 的列表
【发布时间】:2019-11-15 21:41:22
【问题描述】:

我在 System.Xml.Serialization 中使用XmlSerializer

我有一个由xsi:type 分隔的列表(或实际上是两个列表)。

<?xml version="1.0" encoding="utf-8"?>
<ButikerOmbud xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Info>
        <Meddelande>blah blah</Meddelande>
    </Info>
    <ButikOmbud xsi:type="StoreAssortmentViewModel">
        <Typ>Butik</Typ><Nr>2515</Nr>
    </ButikOmbud>
    <ButikOmbud xsi:type="StoreAssortmentViewModel">
        <Typ>Butik</Typ><Nr>2516</Nr>
    </ButikOmbud>
    <ButikOmbud xsi:type="AgentAssortmentViewModel">
        <Typ>Ombud</Typ><Nr>011703-91A</Nr>
    </ButikOmbud>
    <ButikOmbud xsi:type="AgentAssortmentViewModel">
        <Typ>Ombud</Typ><Nr>011703-92B</Nr>
    </ButikOmbud>
</ButikerOmbud>

我创建了一些映射到这个的类:

[XmlRoot(ElementName = "ButikerOmbud")]
public class ButiksCollection
{
    [XmlElement(ElementName = "Info")]
    public Info Info { get; set; }

    [XmlElement(ElementName = "ButikOmbud")]
    public List<Butik> Butiker { get; set; }
}

[XmlRoot(ElementName = "ButikOmbud")]
[XmlType(TypeName = "StoreAssortmentViewModel")]
public class Butik
{
    [XmlElement(ElementName = "Typ")]
    public string Typ { get; set; }

    [XmlElement(ElementName = "Nr")]
    public int Nr { get; set; }

}

然后我会这样做

(ButiksCollection)(new XmlSerializer(typeof(ButiksCollection)).Deserialize(memoryStream));

如果只有 StoreAssortmentViewModel 存在,这应该可以工作。但是鉴于在同一节点下存在AgentAssortmentViewModel。我不知道我应该如何反序列化这个。我假设ButiksCollection 上应该有另一个集合List&lt;Butik&gt; Agents

我发现的唯一似乎映射到xsi:type 的属性应用于类,我认为这不是我想要的

我如何安排和分配我的类以便反序列化?

这是 dotnetfiddle 上的所有内容:https://dotnetfiddle.net/vmT4SK

【问题讨论】:

标签: c# xml xmlserializer


【解决方案1】:

尝试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(ButiksCollection));
            ButiksCollection butik = (ButiksCollection)serializer.Deserialize(reader);
            List<StoreAssortmentViewModel> storeAssortments = butik.Butiker.Where(x => x.GetType() == typeof(StoreAssortmentViewModel)).Select(x => (StoreAssortmentViewModel)x).ToList();
            List<AgentAssortmentViewModel> agentAssortments = butik.Butiker.Where(x => x.GetType() == typeof(AgentAssortmentViewModel)).Select(x => (AgentAssortmentViewModel)x).ToList();

        }
    }
    [XmlRoot(ElementName = "ButikerOmbud")]
    public class ButiksCollection
    {
        [XmlElement(ElementName = "Info")]
        public Info Info { get; set; }

        [XmlElement(ElementName = "ButikOmbud")]
        public List<Butik> Butiker { get; set; }
    }

    [XmlRoot(ElementName = "ButikOmbud")]
    [XmlInclude(typeof(StoreAssortmentViewModel))]
    [XmlInclude(typeof(AgentAssortmentViewModel))]
    public class Butik
    {   
        [XmlElement(ElementName = "Typ")]
        public string Typ { get; set; }
        [XmlElement(ElementName = "Nr")]
        public string Nr { get; set; }

    }
    public class Info
    {
        public string Meddelande { get; set; }
    }
    public class StoreAssortmentViewModel : Butik
    {
    }
    public class AgentAssortmentViewModel : Butik
    {
    }
}

【讨论】:

  • 酷,干杯,这似乎有效,谢谢。 dotnetfiddle.net/qzFNKo 如果我想将它们反序列化为两个单独的列表,我可以这样做吗?
  • 啊,完美,我想我必须将解析模型和域模型分开,然后再拆分它们。并在它们之间复制。该死。感谢您的帮助 jdweng!
猜你喜欢
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 2014-05-04
相关资源
最近更新 更多