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/
分类:
Linq
标签:
Linq to xml
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端