WebClient下载的应用程序数据不会将结果集返回给读者

时间:2016-06-21 10:51:57

标签: c# webforms webclient

我遇到了这个代码的一个主要问题我试图简单地从craftycllick服务器下载一个地址列表,这是基于xml我正在使用webclient进行下载但是当我去浏览器时数据是失败的

if (millis() > precedente + savingtimestep) {
   txt[0] = "ncols         600\nnrows         480\nxllcorner     0\nyllcorner     0\ncellsize      1\nNODATA_value  10\n" +kinectDEM;
   saveStrings("kinectDEM0.tmp", txt);
   precedente = millis();
   //  delete the old .txt file, from kinectDEM1 to kinectDEMtrash
   File f = new File(sketchPath("kinectDEM1.txt"));
   boolean success = f.delete();

   //  rename the old .txt file, from kinectDEM0 to kinectDEM1
   File oldName1 = new File(sketchPath("kinectDEM0.txt"));
   File newName1 = new File(sketchPath("kinectDEM1.txt"));
   oldName1.renameTo(newName1);

   //  rename kinectDEM0.tmp file to kinectDEM0.txt
   File oldName2 = new File(sketchPath("kinectDEM0.tmp"));
   File newName2 = new File(sketchPath("kinectDEM0.txt"));
   oldName2.renameTo(newName2);
 }

返回的Xml是

    DrawRectangle(fill, pen, rect);

我用来解析的例程。

 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)
        {
            return null;
        }
}

以上函数的My Calling代码是

  <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>

修改1  确定我已经建议我更改我的代码以使用using statment并读取其中的内容但我收到了另一个错误,但仍然没有产生数据。

public DataTable returnAddressList(string postcode, string accessCode)
    {
        try
        {
            DataTable dtReturn = new DataTable();
            dtReturn.Columns.Add("PropertyItem", Type.GetType("System.String"));
            dtReturn.Columns.Add("PropertyValue", Type.GetType("System.String"));

            postcode = postcode.Replace(" ", "");
            XmlTextReader reader = readXML(postcode, "data_formatted", accessCode);

            string option1 = "";
            string option2 = "";

            while (reader.Read())
            {

                if (reader.Name.Equals("line_1"))
                {
                    option1 = reader.ReadString();
                }
                if (reader.Name.Equals("udprn"))
                {
                    option2 = reader.ReadString();
                }
                if (option1 != "" && option2 != "")
                {
                    dtReturn.Rows.Add(new object[] { option1, option2 });
                    option1 = "";
                    option2 = "";
                }
            }

            return dtReturn;
        }

        catch (Exception ex)
        {
            return null;
        }
    }

调试的屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:1)

正如评论中的讨论一样......

using (TextReader textReader = new StreamReader(responseStream))
                {
                    XmlTextReader reader = new XmlTextReader(textReader);
                    Debug.Assert(reader != null, "Reader is NULL");
                    return reader;
                }

您创建一个TextReader对象,稍后在XmlTextReader阅读器的构造函数中使用...返回XmlTextReader后退出使用块时,您将处置并关闭textReader,因此您无法从中读取错误一个封闭的textReader。问题在于架构imo。

您应该尝试将while循环逻辑从Parse方法中拉入使用块,例如....

using(TextReader textReader = new StreamReader(responseStream))
        {
            XmlTextReader reader = new XmlTextReader(textReader);

            while(reader.Read())
            {
                if(reader.Name.Equals("line_1"))
                {
                    option1 = reader.ReadString();
                }
                if(reader.Name.Equals("udprn"))
                {
                    option2 = reader.ReadString();
                }
                if(option1 != "" && option2 != "")
                {
                    dtReturn.Rows.Add(new object[] { option1, option2 });
                    option1 = "";
                    option2 = "";
                }
            }
        }

答案 1 :(得分:0)

行。当您想要退回流时,您无法从using statment返回,因为当您退出using时,您调用Dispose方法并关闭该流。

首先,您必须更改readXML方法。

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

        DataTable dataTableResult  = null;

        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");

                        dataTableResult = returnAddressList(postcode, response, reader)
                    }
                }
                throw new Exception("ResponseStream is NULL");
            }

           return dataTableResult;
        }
        catch (WebException ex)
        {
            return null;
        }

接下来您必须更改returnAddressList方法的定义以使用来自此参数的流:

   public DataTable returnAddressList(string postcode, string accessCode, XmlTextReader reader)

并删除了您调用readXML方法

的行
  XmlTextReader reader = readXML(postcode, "data_formatted", accessCode); - delete this line

这不是很好的重构,但你可以看到该做什么以及问题出在哪里。