当前位置: 首页 > 编程语言 > C#

分享_118

时间:2023-04-10 11:46:51 C#

使用RESTXMLWeb服务使用RESTXMLWeb服务我正在尝试使用以下Web服务http://ipinfodb.com/ip_location_api.php该Web服务返回xml响应,下面的代码获取XML响应,但不知何故,当从XML响应中分阶段处理值时它不起作用。我的代码出了什么问题?使用System.Collections.Generic;使用系统文本;使用System.Web;使用System.IO;使用System.Net;使用System.Xml;namespaceConsoleApplication3{classProgram{staticvoidMain(string[]args){HttpWebRequestrequest=null;HttpWebResponse响应=null;字符串Xml;//创建网络请求request=WebRequest.Create("http://api.ipinfodb.com/v2/ip_query.php?key=&ip=74.125.45.100&timezone=true")asHttpWebRequest;//获取响应使用(response=request.GetResponse()asHttpWebResponse){//获取响应流StreamReaderreader=newStreamReader(response.GetResponseStream());Xml=reader.ReadToEnd();}//控制台xml输出Console.WriteLine(Xml);//看看我们是否得到了xml响应,(是的,我们得到了)Console.ReadLine();字符串_currentField="";StringReader_sr=newStringReader(Xml);XmlTextReader_xtr=newXmlTextReader(_sr);_xtr.XmlResolver=null;_xtr.WhitespaceHandling=空白处理。无;//获取根节点_xtr.Read();if((_xtr.NodeType==XmlNodeType.Element)&&(_xtr.Name=="Response")){while(_xtr.Read()){if((_xtr.NodeType==XmlNodeType.Element)&&(!_xtr.IsEmptyElement)){_currentField=_xtr.Name;_xtr.Read();if(_xtr.NodeType==XmlNodeType.Text){switch(_currentField){case"Status":Console.WriteLine(_xtr.Value);//我们出于测试目的打印到控制台,通常在这里将其分配给一个变量!休息;案例“国家代码”:Console.WriteLine(_xtr.Value);休息;case"CountryName":Console.WriteLine(_xtr.Value);休息;case"RegionCode":Console.WriteLine(_xtr.Value);休息;case"RegionName":Console.WriteLine(_xtr.Value);休息;案例“城市”:Console.WriteLine(_xtr.Value);休息;case"ZipPostalCode":Console.WriteLine(_xtr.Value);休息;案例“纬度”:Console.WriteLine(_xtr.Value);休息;案例“经度”:Console.WriteLine(_xtr.Value);休息;案例“Gmtoffset”:Console.WriteLine(_xtr.Value);休息;案例“Dstoffset”:Console.WriteLine(_xtr.Value);休息;case"TimezoneName":Console.WriteLine(_xtr.Value);休息;案例“Isdst”:Console.WriteLine(_xtr.Value);休息;case"Ip":Console.WriteLine(_xtr.Value);休息;默认值://未知字段抛出新异常(“响应中的未知字段。”);}}}}}Console.ReadLine();编辑:这是返回的XML响应-OKUSUnitedStates06CaliforniaMountainView9404337.4192-122.057-288000America/Los_Angeles074.125.45.100我使用相同的API,将响应XML加载到XDocument中并解析例如//在运行时构建URLstringapiKey=ConfigurationManager.AppSettings["geoApiKey"];字符串url=String.Format(ConfigurationManager.AppSettings["geoApiUrl"],apiKey,ip);WebRequest请求=WebRequest.Create(url);尝试{WebResponseresponse=request.GetResponse();使用(varsr=newSystem.IO.StreamReader(response.GetResponseStream())){XDocumentxmlDoc=newXDocument();尝试{xmlDoc=XDocument.Parse(sr.ReadToEnd());stringstatus=xmlDoc.Root.Element("Status").Value;Console.WriteLine("响应状态:{0}",status);if(status=="OK"){//如果状态正常,通常可以安全地假设所需的元素//在那里。但是,如果您想安全起见,您始终可以在检索值之前检查元素//是否存在Console.WriteLine(xmlDoc.Root.Element("CountryCode").Value);Console.WriteLine(xmlDoc.Root.Element("CountryName").Value);...}}catch(Exception){//必要时处理}}}catch(WebException){//必要时处理}你还应该做的是引入一个自定义类,如GeoLocationInfo,并将你的代码包装在一个函数中,如GetGeoLocation(字符串ip)然后您可以填充并返回该类的实例,而不是将信息写入控制台窗口我的解决方案是:在其MSDN文档页面上阅读有关xsd.exe的更多信息。您假设第一个节点将是根节点,但这是不正确的。您将首先拥有XmlDeclaration节点,然后可能是Whitespace节点。因此,您应该将代码结构化为...boolisRootRead=false;while(_xtr.Read()){if(_xtr.NodeType==XmlNodeType.Element){if(!isRootRead){if(_xter.Name=="Response"){//找到根isRootRead=true;}//如果根节点跳转到下一个节点/忽略其他节点,直到读取根元素继续;}_currentField=_xtr.Name;_xtr.Read();if(_xtr.NodeType==XmlNodeType.Text){switch(_currentField){case"Status":Console.WriteLine(_xtr.Value);//我们出于测试目的打印到控制台,通常在这里将其分配给一个变量!休息;...但话虽如此,我个人更喜欢创建响应XSD(如果Web服务提供它则更好)并从中生成类(使用XSD.exe或Xsd2Code)以对其进行序列化/反序列化。我认为你需要使用_xtr.MoveToContent();使用read方法之前的方法。。看看行不行以上就是C#学习教程:使用RESTXMLWeb服务分享所有内容,如果对大家有用还需要了解更多C#学习教程,我希望大家多多关注~本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: