asp.net读取xml接口

      //发送获取xml请求
        public static string SentRequest(String url)
        {
            HttpWebRequest req = WebRequest.CreateHttp(url);
            req.Method = "GET";
            HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
            Stream stream = resp.GetResponseStream();
            StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(resp.CharacterSet));
            String json = reader.ReadToEnd();
            stream.Close();
            return json;
        }
     //获取xml转化格式     
public static string GetXmlStr(string url) { string jsonstr = SentRequest(url); string json1 = jsonstr.Replace("&lt;", "<"); string json2 = json1.Replace("&gt;", ">"); return json2; }
    
    string strXml = WebService.GetXmlStr(url);
    List
<Model> ls = new List<Model>();
    
if (strXml.Contains("系统标识错误")) { ls = null; } else { XmlDocument xx = new XmlDocument(); xx.LoadXml(strXml); XmlNodeList xxlist = xx.GetElementsByTagName("xkxx"); foreach (XmlNode xxNode in xxlist) { Model model = new Model(); XmlNodeList childlist = xxNode.ChildNodes; model .name= childlist[0].InnerText; ls.Add(model); } ViewBag.UserModel= ls; }

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。