Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

XML to be deserialized:

<CheckOnlineStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Lifecare.LIS.Integration.Hyland.Models">
  <Database>string</Database>
  <InstancesStillNearline>0</InstancesStillNearline>
  <SeriesLocationMap xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
    <d2p1:KeyValueOfstringboolean>
      <d2p1:Key>test</d2p1:Key>
      <d2p1:Value>true</d2p1:Value>
    </d2p1:KeyValueOfstringboolean>
    <d2p1:KeyValueOfstringboolean>
      <d2p1:Key>test1</d2p1:Key>
      <d2p1:Value>false</d2p1:Value>
    </d2p1:KeyValueOfstringboolean>
  </SeriesLocationMap>
  <Status>string</Status>
  <StudyInstanceUID>string</StudyInstanceUID>
  <TotalClips>0</TotalClips>
  <TotalExecutionTime>0</TotalExecutionTime>
  <TotalInstances>0</TotalInstances>
</CheckOnlineStatus>

Data Models used:

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

[XmlRoot("CheckOnlineStatus")]
/// <summary>
/// The CheckOnlineStatus entity.
/// </summary>
public class CheckOnlineStatus
{
    [XmlElement("Status")]
    /// <summary>
    /// Gets or sets the Status
    /// </summary>
    public string Status { get; set; }

    [XmlElement("Database")]
    /// <summary>
    /// Gets or sets the Database
    /// </summary>
    public string Database { get; set; }

    /// <summary>
    /// Gets or sets the StudyInstanceUID
    /// </summary>
    [XmlElement("StudyInstanceUID")]
    public string StudyInstanceUID { get; set; }

    /// <summary>
    /// Gets or sets the TotalExecutionTime
    /// </summary>
    [XmlElement("TotalExecutionTime")]
    public long TotalExecutionTime { get; set; }

    /// <summary>
    /// Gets or sets the TotalInstances
    /// </summary>
    [XmlElement("TotalInstances")]
    public int TotalInstances { get; set; }

    /// <summary>
    /// Gets or sets the TotalClips
    /// </summary>
    [XmlElement("TotalClips")]
    public int TotalClips { get; set; }

    /// <summary>
    /// Gets or sets the InstancesStillNearline
    /// </summary>
    [XmlElement("InstancesStillNearline")]
    public int InstancesStillNearline { get; set; }

    /// <summary>
    /// Gets or sets the dictionary of series location map
    /// </summary>
    [XmlElement("SeriesLocationMap")]
    public Dictionary<string, bool> SeriesLocationMap { get; set; }
}

Deserialization code, here result is above xml in string format:

(CheckOnlineStatus)new XmlSerializer(typeof(CheckOnlineStatus)).Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)));

I am getting below runtime error:

Cannot serialize member Lifecare.LIS.Integration.NDP.Models.CheckOnlineStatus.SeriesLocationMap of type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.

I tried couple of other things but it did not worked out e.g. https://www.codeproject.com/Questions/716556/XML-deserialize-Attributes-to-dictionary

Can someone please help here. Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
542 views
Welcome To Ask or Share your Answers For Others

1 Answer

As the exception (InvalidOperationException with an inner UnsupportedException) says you can't simply deserialize it into a Dictionary.

Here is one way to overcome of this difficulty:

Define KeyValue

[Serializable]
public struct KeyValue
{
    public string Key { get; set; }
    public bool Value { get; set; }
}

As an alternative you might take advantage of the built-in KeyValuePair<TKey, TValue>

Use XmlArray and XmlArrayItem

[XmlArray("SeriesLocationMap")]
[XmlArrayItem("KeyValueOfstringboolean")]
public List<KeyValue> IntermediateContainer { get; set; }

Use XmlIgnore to convert List to Dictionary<string, bool>

[XmlIgnore]
public Dictionary<string, bool> SeriesLocationMap  => IntermediateContainer.ToDictionary(x => x.Key, y => y.Value);

I've tested it without namespaces, so you need to specify them at the right place to be able to read them properly.


Alternatively you might give it a try and use DataContractSerializer which does support Dictionary.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...