The weather forecast is obtained by Web Service and read

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Environmental Science: Visual Studio 2010 C#


1 new project --> a Windows forms application. The input solutions, assumed to be TTS.


2 in the interface design. A textBox and a Button. TextBox1 used to contain text information, click the button1 trigger events and read access to weather.


3 in “ the solution explorer, ” in the TTS solution on the right --> add service reference, while the following interface:

“ senior &rdquo, appears the following interface;:

Then add a reference to Web “ ”, pop up the following interface:

Enter in the address bar

-------------------------------------- division line start----------------------------------------------------

[Digression]

This is the weather in China to provide Web services. On the home page of the site, also can find other such as mobile phone numbers attribution to inquiries, flight schedule, exchange rate, English Chinese translation, QQ is online, the code image generating Web service.

In addition several abroad to provide Web Service site (not tried, I do not know can not):

·Special about map Web service:

http://www.opengeospatial.org/standards/wms

·A very rich Web services site:

http://www.programmableweb.com/apis/directory/1?sort=category

-------------------------------------- division line end----------------------------------------------------


Note 1: now the window title is “ add a Web reference to the &rdquo. The beginning is “ add service reference ”. These two are not the same. Although in the first step, enter the above address, in the address bar to, also can look up the Web service, but added to the project, the back with a problem, cannot find the class and method need (see behind the code). May be I will not use the &ldquo service references ”. Anyway, I only know how to use the &ldquo Web reference;”.

Note 2: pay attention to the &ldquo Web reference name; ”, equivalent to adding a Web reference namespace. You can change your.


“ add references to ” after the weather forecast, Web Service will add to our project, can be in “ the solution explorer ” see, as shown in Fig.:


4 the next step is how to use the weather web service.

Add the response function for button1 button1_Click. See specific code:

 private void button1_Click(object sender, EventArgs e)
 {           
    // get weather
    string city = "Wuhan";
    TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();
    string[] wwsArray = wws.getWeatherbyCityName(city);
    this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] 
    + " " + wwsArray[6] + ". " + Environment.NewLine
    + "\r\n" + wwsArray[10]  + ". "+ Environment.NewLine + wwsArray[11];                      
 }

From the Web Service access to information on an array of strings in wwsArray[], I chose some string into textBox1, wwsArray[0] said “ Hubei ” wwsArray[1] &ldquo &rdquo, said; wuhan;. You can also put the entire string array in textBox1, but note that some information is a picture of a city name, like wwsArray[2].


5 read the weather forecast function add.

First look at the C drive under sapi.dll, as shown in the diagram below me, have the DLL two directory:

Remember the file directory, just use that line, two is the same.

And then in the Visual Studio 2010, in “ Solution Explorer in &rdquo project; TTS right --> add reference, pop up as below:


In the browse tab selected we found in the C before sapi.dll, point to determine. Add in. Can be seen in the project, as shown below:

It is called“SpeechLib”.


6 in the code using the SpeechLib Library.

The complete code eventually.:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
 
namespace TTS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        
        private void button1_Click(object sender, EventArgs e)
        {           
            // speak weather
            try
            {
                SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SpVoice voice = new SpVoice();
                if (this.textBox1.Text.Trim() == string.Empty)
                {
                    // get weather
                    string city = "Wuhan";
                    TTS.cn.com.webxml.www.WeatherWebService wws = new cn.com.webxml.www.WeatherWebService();
                    string[] wwsArray = wws.getWeatherbyCityName(city);                    
                    this.textBox1.Text = wwsArray[0] + " " + wwsArray[1] + " " + wwsArray[5] + " " + wwsArray[6] + ". " + Environment.NewLine
                    + "\r\n" + wwsArray[10]  + ". "+ Environment.NewLine + wwsArray[11];                  
                    voice.Speak(this.textBox1.Text, spFlags);
                }
                else
                {
                    voice.Speak(this.textBox1.Text, spFlags);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }        
    }
}


Generation of operation effect.:


At the same time text information can be heard a female voice in reading, read groups of words what is intelligence.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Marsh at November 17, 2013 - 1:22 PM