linq to xml运用示例
代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using XFK.LYTravel.Common.helper; using XFK.LYTravel.Infrastructure; using SFast; using System.Xml.Linq; using XFK.LYTravel.Common; namespace XFK.LYTravel.LYTravelApi.Areas.AppMain.Controllers { public class WXUnifiedOrderPayController : ApiController { public ResponseBase<WXUnifiedPayResp> Post(WXUnifiedPayReq req) { AppMainLogHelper.WriteLog("-----进入AppMain-WXUnifiedOrderPay接口!(/api/AppMain/WXUnifiedOrderPay)------"); AppMainLogHelper.WriteLog("入参:" + req._ToJsonStr()); ResponseBase<WXUnifiedPayResp> result = new ResponseBase<WXUnifiedPayResp>(); XElement xe = new XElement("xml", new XElement("appid", req.appid), new XElement("attach", req.attach), new XElement("body", req.body), new XElement("mch_id", req.mch_id), new XElement("nonce_str", req.nonce_str), new XElement("notify_url", req.notify_url), new XElement("openid", req.openid), new XElement("out_trade_no", req.out_trade_no), new XElement("spbill_create_ip", req.spbill_create_ip), new XElement("total_fee", req.total_fee), new XElement("trade_type", req.trade_type), new XElement("scene_info", req.scene_info), new XElement("sign", req.sign) ); string xmlStr = xe.ToString(); string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; AppMainLogHelper.WriteLog(string.Format("发送微信统一下单接口,地址:{0},参数:{1}", url, xmlStr)); string strResp = TrainTicketAPI.PostWebRequest(url, xmlStr); AppMainLogHelper.WriteLog("返回数据:" + strResp); XElement xeRet = XElement.Parse(@strResp); WXUnifiedPayResp resp = new WXUnifiedPayResp(); resp.return_code = xeRet.Descendants("return_code").First().Value; resp.return_msg = xeRet.Descendants("return_msg").First().Value; if (resp.return_code == "SUCCESS") { resp.appid = xeRet.Descendants("appid").First().Value; resp.mch_id = xeRet.Descendants("mch_id").First().Value; resp.nonce_str = xeRet.Descendants("nonce_str").First().Value; resp.sign = xeRet.Descendants("sign").First().Value; resp.result_code = xeRet.Descendants("result_code").First().Value; if (resp.result_code == "SUCCESS") { resp.prepay_id = xeRet.Descendants("prepay_id").First().Value; resp.trade_type = xeRet.Descendants("trade_type").First().Value; resp.mweb_url = xeRet.Descendants("mweb_url").First().Value; } result.resultCode = "200"; result.IsSuccess = true; result.Message = resp.return_msg; result.ResultData = resp; } else { result.resultCode = "201"; result.IsSuccess = false; result.Message = resp.return_msg; result.ResultData = null; } return result; } } public class WXUnifiedPayReq { public string appid { get; set; } public string attach { get; set; } public string body { get; set; } public string mch_id { get; set; } public string nonce_str { get; set; } public string notify_url { get; set; } public string openid { get; set; } public string out_trade_no { get; set; } public string spbill_create_ip { get; set; } public string total_fee { get; set; } public string trade_type { get; set; } public string scene_info { get; set; } public string sign { get; set; } } public class WXUnifiedPayResp { public string return_code { get; set; } public string return_msg { get; set; } public string appid { get; set; } public string mch_id { get; set; } public string nonce_str { get; set; } public string sign { get; set; } public string result_code { get; set; } public string prepay_id { get; set; } public string trade_type { get; set; } public string mweb_url { get; set; } } }
参考:
http://skybirdzw.blog.163.com/blog/static/7257062620140244456725/
http://skybirdzw.blog.163.com/blog/static/72570626201402445651865/