Search code examples
c#webrequestxml-deserialization

Get xml from website by webrequest then deserialize it and show in console


Hello i learning about this stuff an i need to send request to site get xml in response than deserialize it and see anything what was inside... i created request deserialize method and stream method and Xml Schema but now i dont know what next and it doest working so if anybody know about some nice tutorial pls give me link.

public static class LoadXml
{
    public static root material;



    public static void LoadXML()
    {
        var serviceUrl = "http://api.deezer.com/2.0/artist/27&output=xml";
        string serviceName = "Deezer";

        HttpWebRequest request = null;
        WebResponse response = null;

        request = WebRequest.Create(serviceUrl) as HttpWebRequest;
        request.Method = "GET";
        request.ContentType = " text/xml";



        material = Deserialize<root>(GetResponseStream(request, response, serviceName));

        Console.WriteLine(material.ToString());
    }

    public static T Deserialize<T>(MemoryStream stream)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        T result = (T)serializer.Deserialize(stream);

        return result;
    }

    public static MemoryStream GetResponseStream(HttpWebRequest request, WebResponse response, string debugServiceName)
    {

        response = request.GetResponse();

        MemoryStream stream = new MemoryStream();
        response.GetResponseStream().CopyTo(stream);
        stream.Position = 0;

        return stream;
    }

}

Solution

  • Place (or load) the results into an [XDocument][1] and work with it from there by manipulating and extracting data from the document.

    Using the url as a starting point this is a snippet (easier way to load; try it) where we load it and then look at a target child node if the structure returned was (\root\name):

    XDocument doc = XDocument.Load(@"http://api.deezer.com/2.0/artist/27&output=xml");
    
    Console.WriteLine ( doc.ToString() );
    
    /*
    <root>
      <id><![CDATA[27]]></id>
      <name><![CDATA[Daft Punk]]></name>
      <link><![CDATA[http://www.deezer.com/artist/27]]></link>
      <picture><![CDATA[http://api.deezer.com/2.0/artist/27/image]]></picture>
      <nb_album><![CDATA[54]]></nb_album>
      <nb_fan><![CDATA[592180]]></nb_fan>
      <radio><![CDATA[1]]></radio>
      <type><![CDATA[artist]]></type>
    */
    
    Console.WriteLine ( doc.Element("root").Element("name").Value);
    // Outputs:
    // Daft Punk
    

    Update Alternate Load (Parse)

    var xml = @"
    <root>
      <id><![CDATA[27]]></id>
      <name><![CDATA[Daft Punk]]></name>
      <link><![CDATA[http://www.deezer.com/artist/27]]></link>
      <picture><![CDATA[http://api.deezer.com/2.0/artist/27/image]]></picture>
      <nb_album><![CDATA[54]]></nb_album>
      <nb_fan><![CDATA[592180]]></nb_fan>
      <radio><![CDATA[1]]></radio>
      <type><![CDATA[artist]]></type>
    </root>";
    
    var doc = XDocument.Parse( xml );
    
    Console.WriteLine ( doc.Element("root").Element("name").Value);
    // Outputs
    // Daft Punk