C# 无法在return语句中返回xml
我仍在尝试从webservuce创建以下代码,但由于某些原因,当返回值为null时,即使在调试xml字符串值时,返回值仍为indeadC# 无法在return语句中返回xml,c#,xml,C#,Xml,我仍在尝试从webservuce创建以下代码,但由于某些原因,当返回值为null时,即使在调试xml字符串值时,返回值仍为indead public XmlTextReader readXML(string postcode, string response, string accessCode) { WebRequest wrURL; Stream objStream; string strURL; string url = "http://pcls1.cra
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 ????#####
}
编辑
我在这里仍然没有得到响应,但是当我在实际的浏览器中从代码中生成的url查看它时,它会显示以下内容
<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>
用于显示调试的动画gif
< P>当使用<代码> IDISPABLE < /Cord>对象时,应考虑使用<代码>使用
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
}
如果提供的代码修复了它,请尝试
编辑:
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;
}
}
要缩短代码,您可以使用:
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
编辑:
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;
}
}
你能试试这个片段吗
旁注:
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;
}
}
XmlTextReader
中还有另一个重载,如下所示:
公共XmlTextReader(字符串url)代码>
你也可以试试这个 请让我们也提供输入字符串,以便我们可以看到您代码中的突出部分。此外,您不应该执行new WebClient()。下载字符串(url)
,因为WebClient
是IDisposable
的,所以您必须保留一个要处理的引用。对我来说,这还不清楚。您是如何发现读卡器为空的?读卡器是否为空?还是读者的阅读没有任何回报?它是读取null
还是读取空字符串?请提供一个。请查看我在上面所做的编辑。您是否可以尝试Hi使用(WebRequest wrURL=WebRequest.Create(url))此处有错误说它不能转换为IDisposable?@david yeah我的失败-.-WebRequest
没有实现IDisposable
,所以你不能使用使用-我修复了我的答案,但仍然没有返回任何信息-(@David,你真的想返回XmlTextReader?或者简单地返回你在响应中得到的xml?它说读卡器为空,但我不明白那蜜蜂怎么可能不在防火墙上?当我将其放入浏览器url时,所有端口80流量都是允许的