C # Implementation of Chinese Weather Network XML Interface test

Source: Internet
Author: User

Click on the link to see China Weather Network interface Description, recently want to study the interface test, from the most recent talk with a company's technical director (Jiaotong University alumni), found that the requirements of interface testing is relatively large, so want to study.

Finally found on the internet on the Chinese Weather Network interface Description, is the above article. Decide to access this interface, analyze the content returned by the interface, and filter out the information I want.

The thought of the interface was mainly JSON type, but I did not find the JSON type interface (above the JSON interface seems to be invalid), first take the XML type interface test, I think the principle should be similar, but the specific implementation is somewhat different.

If anyone has a JSON interface available for easy disclosure, welcome message, thank you very much!

The following is an example of the weather information for Beijing city from the content returned from the China Weather Network interface, the code is as follows:

usingSystem;usingSystem.Linq;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Xml.Linq;namespaceinterfacetest{classProgram {Static voidMain (string[] args) {Reportweather ("Beijing"); }        Private Static voidReportweather (stringCityName) {            stringLocalcontent = Environment.getfolderpath (Environment.SpecialFolder.DesktopDirectory) +@"\test.txt"; Try            {                stringURL ="Http://flash.weather.com.cn/wmaps/xml/china.xml"; WebClient mywebclient=NewWebClient ();byte[] pagedata = mywebclient.downloaddata (URL);stringpagehtml = Encoding.UTF8.GetString (pagedata);
//XDocument doc = Xdocument.parse (pagehtml); using(StreamWriter SW =NewStreamWriter (localcontent)) {SW. WriteLine (pagehtml); } XDocument Doc=Xdocument.parse (pagehtml); //XDocument doc = xdocument.load (localcontent); varResults = fromEleinchDoc. Descendants (" City")whereEle. Attribute ("CityName"). Value.equals (CityName)SelectEle; foreach(varResultinchresults) {Console.WriteLine ("City:"+cityname); Console.WriteLine ("Weather conditions:"+ result. Attribute ("statedetailed"). Value); Console.WriteLine ("temperature conditions (degrees Celsius):"+ result. Attribute ("tem1"). Value +"to the"+ result. Attribute ("tem2"). Value); Console.WriteLine ("Wind Conditions:"+ result. Attribute ("windstate"). Value); } console.readline (); } Catch(WebException webEx) {Console.WriteLine (webEx.Message.ToString ()); } } }}

For the sake of convenience, I saved the information returned by the interface to a local copy (path Localcontent), which reads as follows:

Run as follows (cloudy turn haze ...) I remember listening to the weather forecast is cloudy and sunny ... Alas...... ):

This allows us to filter the return information of the XML interface and test the XML type interface.

C # Implementation of Chinese Weather Network XML Interface test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.