C#调用WebService并解析返回的XML

        string title = HttpUtility.UrlEncode(txtTitle.Text);
        string content = HttpUtility.UrlEncode(content1.Value);

        string postData = "title=" + title + "&content=" + content;
        byte[] dataBytes = Encoding.UTF8.GetBytes(postData);

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        string url = System.Configuration.ConfigurationManager.AppSettings["KeywordWebService"];
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = dataBytes.Length;
        request.Method = "POST";

        Stream postStream = request.GetRequestStream();
        postStream.Write(dataBytes, 0, dataBytes.Length);
        postStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();

        StreamReader receiveStream = new StreamReader(response.GetResponseStream());
        string receiveString = receiveStream.ReadToEnd();
        sw.Stop();

        xml.LoadXml(receiveString);

        System.Xml.XmlNodeList keywords = xml.SelectNodes("//Keyword");
        string strkeywords = "";
        foreach (System.Xml.XmlNode keyword in keywords)
        {
            System.Diagnostics.Debug.WriteLine(keyword.InnerText);
            strkeywords = strkeywords + keyword.InnerText + " ";
        }

        this.txtSearch.Value = strkeywords.Trim();

你可能感兴趣的:(C#调用WebService并解析返回的XML)