-1

I am still trying to from a webservuce I have the following code crafted, but for some reason when it comes to the return value its null even though when i debug the xml string value it is indead there.

public XmlTextReader readXML(string postcode, string response, string accessCode)
{
   WebRequest wrURL;
   Stream objStream;
   string strURL;
   string url = "http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode=" + postcode + "&response=" + response + "&key=" + accessCode;
   wrURL = WebRequest.Create(url);
   string xml = new WebClient().DownloadString(url);

   objStream = wrURL.GetResponse().GetResponseStream();
   StreamReader objSReader = new StreamReader(objStream);
   strURL = objSReader.ReadToEnd().ToString(); #####but here it has the xml data ?####
   XmlTextReader reader = new XmlTextReader(new StringReader(strURL));
return reader;#######here its empty ????#####
 }

enter image description here

Edit

I am still not getting a response here but yet when i view it in the actual browser from the url produced within the code it displays the following

<CraftyResponse><address_data_formatted><delivery_point><organisation_name>THE BAKERY</organisation_name><department_name/><line_1>1 HIGH STREET</line_1><line_2>CRAFTY VALLEY</line_2><udprn>12345678</udprn></delivery_point><delivery_point><organisation_name>FILMS R US</organisation_name><department_name/><line_1>3 HIGH STREET</line_1><line_2>CRAFTY VALLEY</line_2><udprn>12345679</udprn></delivery_point><delivery_point><organisation_name>FAMILY BUTCHER</organisation_name><department_name/><line_1>7 HIGH STREET</line_1><line_2>CRAFTY VALLEY</line_2><udprn>12345680</udprn></delivery_point><delivery_point><organisation_name/><department_name/><line_1>BIG HOUSE, HIGH STREET</line_1><line_2>CRAFTY VALLEY</line_2><udprn>12345681</udprn></delivery_point><delivery_point><organisation_name/><department_name/><line_1>LITTLE COTTAGE</line_1><line_2>17 HIGH STREET, CRAFTY VALLEY</line_2><udprn>12345682</udprn></delivery_point><delivery_point_count>5</delivery_point_count><town>BIG CITY</town><postal_county>POSTAL COUNTY</postal_county><traditional_county>TRADITIONAL COUNTY</traditional_county><postcode>AA1 1AA</postcode></address_data_formatted></CraftyResponse>

I tried method 2 mentioned below but stil no luck

 public XmlTextReader readXML(string postcode, string response, string accessCode)
  {
    string url = $"http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode={postcode}&response={response}&key={accessCode}";
        using (Stream objStream = WebRequest.Create(url)?.GetResponse().GetResponseStream())
        {
            return new XmlTextReader(new StringReader(new StreamReader(objStream)?.ReadToEnd()));
        }//Dispose the Stream            
    }

Animated gif to show debuging

enter image description here

Community
  • 1
  • 1
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

0

When working with IDisposable Objects you should consider using using

public XmlTextReader readXML(string postcode, string response, string accessCode)
{            
   string strURL = string.Empty;
   string url = "http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode=" + postcode + "&response=" + response + "&key=" + accessCode;
   WebRequest wrURL = WebRequest.Create(url);      
   //string xml = new WebClient().DownloadString(url); //What is this for ? I dont see you using this in your code ?!
   using(Stream objStream = wrURL.GetResponse().GetResponseStream())
   {
      using(StreamReader objSReader = new StreamReader(objStream))
      {                
         return new XmlTextReader(new StringReader(objSReader.ReadToEnd()));
      }//Dispose the StreamReader
   }//Dispose the Stream             
 }

Try if the provided code fixes it.

EDIT:

To shorten your code you could use:

string url = $"http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode={postcode}&response={response}&key={accessCode}";    
using(Stream objStream = WebRequest.Create(url)?.GetResponse().GetResponseStream())
{                     
   return new XmlTextReader(new StringReader(new StreamReader(objStream)?.ReadToEnd()));
}//Dispose the Stream

EDIT:

public XmlTextReader ReadXml(string postcode, string response, string accessCode)
    {
        //Create URL
        string url = $"http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode={postcode}&response={response}&key={accessCode}";

        try
        {
            //Create WebRequest
            WebRequest request = WebRequest.Create(url);
            using (Stream responseStream = request.GetResponse().GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (TextReader textReader = new StreamReader(responseStream))
                    {
                        XmlTextReader reader = new XmlTextReader(textReader);
                        Debug.Assert(reader != null, "Reader is NULL");
                        return reader;
                    }
                }
                throw new Exception("ResponseStream is NULL");
            }
        }
        catch (WebException ex)
        {
            //Handle exceptions here
            throw;
        }
    }

Can you try this snippet ?

Side Note:

There is another overload in XmlTextReader which looks like this:

public XmlTextReader(string url);

public XmlTextReader readXML(string postcode, string response, string accessCode)
{
    //Create URL
    string url = $"http://pcls1.craftyclicks.co.uk/xml/rapidaddress?postcode={postcode}&response={response}&key={accessCode}";
    return new XmlTextReader(url);
}

You can also try this one !

Felix D.
  • 4,811
  • 8
  • 38
  • 72