C# Post请求后t解析API接口返回的XML文档

接口示例http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline

     /// <summary>
     /// HttpPost请求
     /// </summary>
     /// <param name="Url">链接</param>
     /// <param name="DataParam">参数</param>
     /// <returns></returns>
 public string HttpPost(string Url, string postDataStr)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.Timeout = 300000;

            byte[] bytes = Encoding.UTF8.GetBytes(postDataStr);
            request.ContentLength = bytes.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(bytes, 0, bytes.Length);
            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            response.Close();
            return retString;
        }

调用接口返回到 retString 的 XML内容的字符串
retString ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=“http://WebXml.com.cn/”>Y"

 string responseStrFime = new RequestHelper().NowHttpPost(Url, DataParam);
                XmlDocument doc = new XmlDocument();//新建一个XML文档
                doc.LoadXml(responseStrFime);//将字符串转换成XML文档

                XmlNodeList weather_nodes = doc.GetElementsByTagName("string");//读取XML文档的父节点
                string State = weather_nodes[0].InnerText;

此时节点State为Y
结束

更多节点访问方式

 int id = int.Parse(node.ChildNodes[0].InnerText);//InnerText节点包含的所有文本内容
  string lang = node.ChildNodes[1].Attributes[0].InnerText;
  string name = node.ChildNodes[1].InnerText;
   int damage = int.Parse(node.ChildNodes[2].InnerText);

参考以下部分代码
原文链接:https://blog.csdn.net/weixin_33950757/article/details/90410970

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值