• 关于访问链接返回XML的获取数据


    1. 返回DataSet格式;

            /// <summary>
            /// 向某个url提交数据并读取该地址返回的xml,并将xml转换成dataset,并返回dataset中某个表
            /// </summary>
            /// <param name="url">提交的低至</param>
            /// <param name="param">参数</param>
            /// <param name="tableName">表名</param>
            /// <param name="encoding">编码格式</param>
            /// <returns></returns>
            public static DataSet PostData(string url, string param, String tableName, Encoding encoding)
            {
    #region post数据 DataSet ds = new DataSet(); DataTable dt = new DataTable(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Timeout = 120000; try { byte[] requestbytes = encoding.GetBytes(param); req.Method = "post"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = requestbytes.Length; System.IO.Stream requeststream = req.GetRequestStream(); requeststream.Write(requestbytes, 0, requestbytes.Length); requeststream.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), encoding); String backstr = sr.ReadToEnd(); StringReader txtReader = new StringReader(backstr); XmlTextReader xmlReader = new XmlTextReader(txtReader); ds.ReadXml(xmlReader); sr.Close(); res.Close(); sr.Dispose(); } catch { return null; } //条件判断 //if (ds != null && ds.Tables.Count > 0 && ds.Tables[tableName] != null) //{ // dt = ds.Tables[tableName].Copy(); //} if (ds != null && ds.Tables.Count > 0) { // dt = ds.Tables[tableName].Copy(); return ds; } return ds; #endregion }

    2. 返回字符串

                    string url = Config.PassportUserInfoUrl + "&userid=" + userid;//可以把链接放到配置文件中
                    string result = SlRequest.GetResponse(url, "");
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(result);
                    XmlNode node = doc.SelectSingleNode("soufun_passport/common");//选择XML节点的值
                    if (node != null)
                    {
                        string resMsg = node.SelectSingleNode("return_result").InnerText;
                        if (resMsg == "100")
                        {
                           //此处可以定义一个User实体类, 来接收关于User的Inforeturn user;
                        }
                        else
                        {
                            errorMessage = node.SelectSingleNode("error_reason").InnerText;
                            return null;
                        } 
    //此处讲判断某个XML节点下面有几个子节点

                       if (node.ChildNodes.Count > 3)
                       {
                            resMsg = "";
                        }

    
    

     注: C#将XMLDocument转化为String函数

           /// 将XmlDocument转化为string  
           /// </summary>  
           /// <param name="xmlDoc"></param>  
           /// <returns></returns>  
           public string ConvertXmlToString(XmlDocument xmlDoc)  
           {  
               MemoryStream stream = new MemoryStream();  
               XmlTextWriter writer = new XmlTextWriter(stream, null);  
               writer.Formatting = Formatting.Indented;  
               xmlDoc.Save(writer);   
        StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);  
               stream.Position = 0;  
               string xmlString = sr.ReadToEnd();  
               sr.Close();  
               stream.Close();   
        return xmlString;  
           }  
  • 相关阅读:
    Installing OpenSSH from the Settings UI on Windows Server 2019 or Windows 10 1809
    [CB]Intel 2018架构日详解:新CPU&新GPU齐公布 牙膏时代有望明年结束
    [百科]数字孪生
    wifi 标准
    WM_CONCAT和LISTAGG 语法例子
    Informix 启动 Fatal error in shared memory initialization解决方法
    B站日志系统的前世今生
    nginx安全日志分析脚本的编写
    可视化web日志分析工具Logstalgia
    CDH-5.7.0:基于Parcels方式离线安装配置
  • 原文地址:https://www.cnblogs.com/sauerkraut/p/5711019.html
Copyright © 2020-2023  润新知