Question

I have seen this question asked, but have not found answers that work so I am asking it again. I have a restful web service with a POST method that returns a serialized List.

<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>QoL5vA8OKgydWIn%2fdWiu70nobBrJo4N9iXeHmtM7Aj4%3d</string>
<string>vxHyJiSSSvDSZWXOdcfl5FMQC97xxGEWDO8Zy8Qp3X5CwADUbEE8ifACQHR1n7uamEaIUbf85ZuBDB8FFCNY2tJAMJ2jNw09SqGVTpMU5uI06DLtuYnJqsPIF735NOhlvRhBLPnpp62DFMCVsDNHy55UBF3Ggi06ZWTiJ8LTYIf3FYiFLPpXLZ1wWeE5chAWQGfz7zDYJa1OgSZ</string>
<string>OqGAT7Yqe6DfyVD29BeIXFyGtabVCloaC9FA1Fp20JkJ9T17ZzyqhnGxwWda7FqqyJUM8YK9OdcOCgTYrl4JxalECdtJm75TdSG8IAPQlFHp6Gidg4EwZaO9FKahlYMm5JrFpxTmLrdLgMAkYEV7gIR6zIyIByAGwYYDDwH3QCHrym3CuueRnFWAHCJu1LJbx0zRtt7g3yEaTiJ</string>
</ArrayOfstring>

The code performing the call is as follows below.

        XDocument xDocResponse = RestPOSTToXDocument(szBaseUri, szInput);

        string szNamespace = xDocResponse.Root.Name.Namespace.ToString();
        IXmlUtils utility = new XmlUtils();
        List<string> lst = utility.DeserializeList<string>(xDocResponse, szNamespace);

I have also attempted a different version of the call as shown below.

        List<List<string>> lst = utility.DeserializeList<List<string>>(xDocResponse, szNamespace);

Both versions of the calls generate the error provided below.

An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code Additional information: There is an error in XML document (0, 0).

My Deserialize method is provided below.

    public List<T> DeserializeList<T>(XDocument doc, string szNamespace)
    {
        List<T> result = null;
        XmlSerializer serializer = new XmlSerializer(typeof(List<T>), szNamespace);

        using (System.Xml.XmlReader reader = doc.CreateReader())
        {
            result = (List<T>)serializer.Deserialize(reader);
        }

        return result;
    }

The XML within the XDocument is valid, so I do not understand why this error is being generated. Also, even if I attempt to get the elements via navigation in the XDocument, it does not work. If I look at the doc.Root.value, it appears that all of the strings are concatenated together into a single string.

Does anyone have any information on how I may deserialize this XDocument into a List?

Was it helpful?

Solution

Thanks to the assistance of mellamokb, I found the problem. When my List is serialized, it is serialized into an ArrayOfstring. If I take the XML and replace ArrayOfstring with ArrayOfString, the deserialization works.

This does not make much sense. I would welcome an explanation of why this is necessary though it may have something to do with String vs string.

Essentially, I had to do the following:

        string szXml = xDocResponse.ToString();
        int nEndRoot = szXml.IndexOf(">");
        szXml = szXml.Replace("ArrayOfstring", "ArrayOfString");

        xDocResponse = utility.LoadXDocument(szXml);

        string szNamespace = xDocResponse.Root.Name.Namespace.ToString();
        List<string> lst = utility.DeserializeList<string>(xDocResponse, szNamespace);

OTHER TIPS

Simply get the response stream and deserealize into string array and store it to List

Code Snippet:

    List<string> listNew=new List<string>();
     using (Stream answer = webResponse.GetResponseStream())
     {                                              
       DataContractSerializer xmlSer = new DataContractSerializer(typeof(string[]));
       var stringArr= (string[])xmlSer.ReadObject(answer); 
       foreach (string item in GenreList)
        {
          listNew.Add(item);
        }
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top