Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have one web API Service where I am getting XML Response My Web API is like this

public Task<Dictionary<long, string>> GetXMLResponse() 
           {

            Dictionary<long, string> dic = new Dictionary<long, string>();
            dic.Add(1, "a");
            dic.Add(2, "b");

            return dic;
           }


Now I am calling this Service from My Window Application like This

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    if (response != null)
                    {
                        using (var streamReader = new StreamReader(response.GetResponseStream()))
                        {
                           XmlSerializer obj = new XmlSerializer(typeof(List<MyClass>));
                            
                           List<MyClass> class1 = (List<MyClass>)obj.Deserialize(streamReader);
                            
                        }



My Class Is like This

public class MyClass
    {
        [XmlElement(ElementName = "Key")]
        public long Key { get; set; }
  
        [XmlElement(ElementName = "Value")]
        public string Value { get; set; }
    }


But when I am going to deserialize it then I am getting error like

Quote:
There is an error in XML document (1, 2).

and Inner Exception is
Quote:
<arrayofkeyvalueoflongstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> was not expected.


Response What I am Getting Like

<?xml version="1.0"?>

-<ArrayOfKeyValueOflongstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">


-<KeyValueOflongstring>

<Key>1</Key>

<Value>a</Value>

</KeyValueOflongstring>


-<KeyValueOflongstring>

<Key>2</Key>

<Value>b</Value>

</KeyValueOflongstring>

</ArrayOfKeyValueOflongstring>


What I have tried:

First I tried to deserialize directly into dictionary but I was getting error and then I tried to make this class.

Thanks in Advance Please can you help me to deserialize it, By json object it is easy to deserialize but XML response I stucked.

Please Help me.
Thanks in Advance
Posted
Updated 1-Jul-19 21:31pm
v2
Comments
F-ES Sitecore 1-Jul-19 7:09am    
Make GetXMLResponse return List<MyClass> as well. Not all objects (such as the Dictionary) are going to support serialisation. The only way to guarantee you can serialise things is to ensure they have the [Serializable] attribute, or to generate your own classes that you know can be serialised.
Member 10192073 1-Jul-19 7:19am    
Hi, When Same I am trying to get in Json and then deserialize into dictionary that time it is working fine, But I have to implement it by Xml response in my project and Unit Testing.

When Response in Json that time

using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
dic = JsonConvert.DeserializeObject<dictionary<long, string="">>(result);
}

these lines is enough to deserialize json string

1 solution

It might be possible to get the dictionary working indeed - as most experienced programmers I wouldn't know, because no matter if it works or not, I know it should never be used. Dictionaries are implementation details, not something that can natively be represented in JSON or XML so you can never trust how it is serialized.

As suggested, change the controller to return IEnumerable<string>.
Something like:
public IEnumerable<MyClass> GetXMLResponse() 
{
    // Just keeping your dictionary here to demonstrate the backend can still
    // work with a dictionary if this is the implementation making most sense
    Dictionary<long, string> dic = new Dictionary<long, string>();
    dic.Add(1, "a");
    dic.Add(2, "b");
    return dic.Select(entry => new MyClass { Key = entry.Key, Value = entry.Value});
}


In the client you can convert your enumerable of MyClass to a dictionary again by doing something like:
myclassList.ToDictionary(entry => entry.Key, entry => entry.Value);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900