C# Linq方式生成SAP对接的XML格式内容(一般处理程序 ashx )
2024-08-24 16:59:34
Linq生成XML的方法:
string CreateXML(string strkey, string strDATAJSON)
{ XDeclaration dec = new XDeclaration("1.0", "UTF-8", null);
XDocument xdoc = new XDocument();
XNamespace saop = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns0 = "http://www.db.com";
XElement root = new XElement(saop + "Envelope",
new XAttribute(XNamespace.Xmlns + "soap", "http://schemas.xmlsoap.org/soap/envelope/"),
new XElement(saop + "Body",
new XElement(ns0 + "MT_COMMON_RET",
new XAttribute(XNamespace.Xmlns + "ns0", "http://www.db.com"),
new XElement("KEY", strkey),
new XElement("DATAJSON_RET", strDATAJSON))) );
xdoc.Add(root);
string strdoc = dec.ToString();//@"<?xml version=""1.0"" encoding=""utf-8""?>";
return strdoc + xdoc.ToString(); }
生成结果格式:
<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns0:MT_COMMON_RET xmlns:ns0="http://www.db.com"> <KEY>DB_XZQYSJTB</KEY> <DATAJSON_RET>{"MANTD":"","MANTR":""}</DATAJSON_RET> </ns0:MT_COMMON_RET> </soap:Body> </soap:Envelope>
访问方法:
public void ProcessRequest(HttpContext context)
{ int rowcount = (int)context.Request.InputStream.Length;
byte[] buff = new byte[rowcount];
context.Request.InputStream.Read(buff, , rowcount);
string text = System.Text.Encoding.UTF8.GetString(buff);
Log4.WriteLog("Handler非标接收数据:" + text ?? "");
/***********************************************************/
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("AAA", "");
dic.Add("BBB", "");
string strDATAJSON = Common.Tools.ConvertToJsonStr(dic);
string strxml = CreateXML("KEY值", strDATAJSON);
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-Length", strxml.Length.ToString());
context.Response.ContentType = "text/xml";
context.Response.Write(strxml);
context.Response.Flush();
context.Response.End(); }
最新文章
- ASP.NET MVC 3 技术(九) 301永久重定向不带www域名到带www的域名
- 【译】java.lang.ThreadLocal
- 3、eclipse和maven环境安装以及HDFS读写的demo
- UOJ30——【CF Round #278】Tourists
- Servlet,GenericServlet和HttpServlet的继承关系
- UVa 11210 (DFS) Chinese Mahjong
- [Arduino] 外部中断
- K - 计算球体积
- js深入研究之类定义与使用
- css学习笔记二
- 提高你的Java代码质量吧:使用构造函数协助描述枚举项
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(十)SVN搭建
- Android在listview添加checkbox实现单选多选操作问题
- 利用Eclipse中的Maven构建Web项目报错(一)
- Python-读文件
- cf1136E. Nastya Hasn&#39;t Written a Legend(二分 线段树)
- 更好用的excel国际化多语言导出
- 雷林鹏分享:使用 CSS 显示 XML
- spring事务——try{...}catch{...}中事务不回滚的几种处理方式
- poj 3667 线段树