日期:2014-05-18  浏览次数:20526 次

asp.net 获取返回的xml数据中的值
URL = http://*****.com/xxx/xxx/Send?Account=123&Password=123123&Phone=13333333333&Content=内容

上面这个链接提交以后返回的是一个xml的文档,如下格式:
<result>
<response>-2</response>
</result>

我想获取到这个 -2 也就是response中的值,下面的代码没办法获取,请教各位前辈帮我看看,谢谢了!

C# code

 Encoding utf8 = Encoding.UTF8;
        Encoding gbk = Encoding.GetEncoding("gbk");
        
        byte[] tmp = utf8.GetBytes(URL);
    
        byte[] tmp1 = Encoding.Convert(utf8, gbk, tmp);
        
        string result = gbk.GetString(tmp1);
      
        byte[] buffer = Encoding.UTF8.GetBytes(result);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
        ////做请求
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = buffer.Length;
        Stream reqst = req.GetRequestStream();
        reqst.Write(buffer, 0, buffer.Length);
        reqst.Flush();
        reqst.Close();       
        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
        //读结果
        Stream resst = res.GetResponseStream();
        StreamReader sr = new StreamReader(resst);        
        XmlDocument rssDoc = new XmlDocument();
        rssDoc.Load(resst);
        XmlNode rss = rssDoc.SelectSingleNode("result/response");        
        string DHResult = rss.Attributes[0].Value.ToString();
        resst.Close();
        sr.Close();

        return DHResult;   




我希望返回的 DHResult 就是返回的那个 response的值 -2,请教大家帮我调整下,谢谢!

------解决方案--------------------
Stream resst = res.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(resst);
XmlNode rss = rssDoc.SelectSingleNode("/result/response");
string DHResult = rss.InnerText;