/** * @将xml转为WxPayData对象并返回对象内部的数据 * @param string 待转换的xml串 * @return 经转换得到的Dictionary * @throws WxPayException */ public SortedDictionary <string, object> FromXml(string xml) { SortedDictionary <string, object> mValues = new SortedDictionary <string, object>(); if (string.IsNullOrEmpty(xml)) { throw new Exception("将空的xml串转换不合法!"); } SafeXmlDocument xmlDoc = new SafeXmlDocument(); xmlDoc.LoadXml(xml); XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点<xml> XmlNodeList nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; mValues[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中 } if (mValues["return_code"].ToString() != "SUCCESS") { return(mValues); } CheckSign(mValues);//验证签名,不通过会抛异常 return(mValues); }
/// <summary> /// 获取返回数据 /// </summary> /// <returns></returns> public SortedDictionary <string, object> GetReturnData() { SortedDictionary <string, object> dic = new SortedDictionary <string, object>(); StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream); string xmlData = reader.ReadToEnd(); SafeXmlDocument xml = new SafeXmlDocument(); xml.LoadXml(xmlData); XmlNode oNode = xml.DocumentElement; if (oNode != null) { XmlNodeList oList = oNode.ChildNodes; foreach (XmlNode item in oList) { dic.Add(item.Name, item.InnerText); } } return(dic); }