-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
csharpdude77
  • 3,221
  • 3
  • 32
  • 80

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,417
  • 6
  • 37
  • 68
  • Hi there is an error here using (WebRequest wrURL = WebRequest.Create(url)) saying it cannot conver to IDisposable ? – csharpdude77 Jun 20 '16 at 11:55
  • @david yeah my fail -.- `WebRequest` does not implement `IDisposable` so you cannot use `using` - I fixed my answer – Felix D. Jun 20 '16 at 12:09
  • its still returning nothing im afriad ;-( – csharpdude77 Jun 20 '16 at 12:16
  • @David and you really want to return the XmlTextReader ? or simple the xml you got in the response ? – Felix D. Jun 20 '16 at 12:18
  • its saying the reader is null but how can that bee i do not understand im not on a firewalla all port 80 traffic is allow when i put it into browser url works – csharpdude77 Jun 20 '16 at 12:42
  • @david at least we got a hint on where to look ;D I will check that :D – Felix D. Jun 20 '16 at 12:43
  • "I assume your problem is that the GC (GarbageCollector) is throwing your xml string away to early." Why should it? `XmlTextReader` references `StringReader` references `string`. As long as someone holds a reference to the XmlTextReader, the GC should not collect it, right? – Thomas Weller Jun 20 '16 at 12:44
  • @Thomas yeah you're right. This was just my first quess ^^ but it seems to be a bit more tricky. I will update my answer as soon as we've found out the real problem – Felix D. Jun 20 '16 at 12:45
  • @david: "its saying the reader is null" really? A constructor cannot return null. http://stackoverflow.com/questions/438325/can-object-constructor-return-a-null – Thomas Weller Jun 20 '16 at 12:50
  • @Thomas I guess it's the `Debug.Assert` which is triggered. – Felix D. Jun 20 '16 at 12:52
  • @Thomas please see my screen recording gif to see what i mean please in above edit – csharpdude77 Jun 20 '16 at 13:02
  • @david: so `reader` is not `null`, since you can see properties of it. – Thomas Weller Jun 20 '16 at 13:16
  • @Thomas BUT it is at none ie no data being entereed into it – csharpdude77 Jun 20 '16 at 13:25
  • @david: which is a totally different topic. No data != null != NotSupportedException. And: why should there be data in the debugger if nobody ever read from the reader? Do you expect the reader to read everything in advance? – Thomas Weller Jun 20 '16 at 13:33