1

I want to parse XML which I have from WebResponse, I make a request and on the response, I get an XML. This is my response :

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:loginResponse xmlns:ns2="http://tasks.ws.com/">
         <LoginResult>
            <status>1</status>
            <uid>a609fd54-e355b373-fc6a-41a6-b178-0baab0ff944d</uid>
            <mapServerList>     
               <SERVER>
                  <name>OSM Mapnik</name>
                  <URL>http://tile.openstreetmap.org/%zoom%/%x%/%y%.png</URL>
                  <waterMarkText>OpenStreetMap contributors</waterMarkText>
                  <waterMarkURL>http://www.openstreetmap.org/copyright</waterMarkURL>
                  <maxZoom>16</maxZoom>
               </SERVER>
               <SERVER>
                  <name>OpenCycleMap</name>
                  <URL>http://tile.opencyclemap.org/cycle/%zoom%/%x%/%y%.png</URL>
                  <maxZoom>16</maxZoom>
               </SERVER>
               <SERVER>
                  <name>OpenCycleMap Transport</name>
                  <URL>http://tile2.opencyclemap.org/transport/%zoom%/%x%/%y%.png</URL>
                  <maxZoom>16</maxZoom>
               </SERVER>    
            </mapServerList>
         </LoginResult>
      </ns2:loginResponse>
   </S:Body>
</S:Envelope>

I created c# classes :

[XmlType("LoginResult")]
        public class LoginResponse
        {
            public string Uid { get; set; }
            public MapServerList mapServerList { get; set; }
        }

[XmlType("SERVER")]
public class SERVER
{
    string name { get; set; }
    string URL { get; set; }
    int maxZoom { set; get; }
}

[XmlType("mapServerList")]
    public class MapServerList
    {

        List<SERVER> mapServerList { get; set; }
    }

And I tried do this :

XmlSerializer serializer = new XmlSerializer(typeof(LoginResponse));
LoginResponsedeserialized = (LoginResult)serializer.Deserialize(stream);

But it does not work, it always returns null.

marsze
  • 15,079
  • 5
  • 45
  • 61
jpok
  • 143
  • 2
  • 2
  • 11
  • 3
    Your root XML element is an Envelope, not `LoginResult`. Your model doesn't match your XML. –  Dec 03 '18 at 14:07
  • 1
    Check out this question: https://stackoverflow.com/questions/4203540/generate-c-sharp-class-from-xml The tool listed above comes from microsoft, and is the most reliable way to model POCOs according to an XML doc spec. There's also a way to generate with an xml sample as opposed to the XSD. – Neal Dec 03 '18 at 14:09
  • i suggest using an XML convert to create your classes like [this one](https://xmltocsharp.azurewebsites.net/), besides that as it seems your root element is `Envelope` and not `LoginResult` – styx Dec 03 '18 at 14:09
  • I agree with the use of `xsd` instead of your approach. – Hackerman Dec 03 '18 at 14:14
  • also you are deserializing to `XmlSerializer(typeof(LoginResponse))` casting to `LoginResult` afterwards ?! -> `response != result` – Felix D. Dec 03 '18 at 14:14
  • Seems like it's a call from a [soap](https://en.wikipedia.org/wiki/SOAP) endpoint. If it's correct it's easier to generate a proxy class from the wsdl and let the proxy handle the response. – NicoD Dec 03 '18 at 14:41

1 Answers1

1

The following code is tested

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

namespace ConsoleApplication87
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";   
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader stream = new StringReader(xml);

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope deserialized = (Envelope)serializer.Deserialize(stream);
        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement("Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body body { get; set; }
    }
    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement("loginResponse", Namespace = "http://tasks.ws.com/")]
        public LoginResponse loginResponse { get; set; }
    }
    [XmlRoot("loginResponse", Namespace = "http://tasks.ws.com/")]
    public class LoginResponse
    {
        [XmlElement("LoginResult", Namespace = "")]
        public LoginResult loginResult { get; set; }
    }
    [XmlRoot("LoginResult", Namespace = "")]
    public class LoginResult
    {
        [XmlElement("uid")]
        public string Uid { get; set; }
        [XmlElement("mapServerList")]
        public MapServerList mapServerList { get; set; }
    }

    [XmlRoot("SERVER", Namespace = "")]
    public class SERVER
    {
        public string name { get; set; }
        public string URL { get; set; }
        public int maxZoom { set; get; }
    }

    [XmlRoot("mapServerList", Namespace = "")]
    public class MapServerList
    {
        [XmlElement("SERVER")]
        public List<SERVER> mapServerList { get; set; }
    }


}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Create a new project and paste my code into new project. It should work. I made a lot of changes to your original code and classes and it may not be obvious all the changes I made. Which line is giving the exception? – jdweng Dec 04 '18 at 08:07