XML 字符串解析
微信红包发送完成后返回xml字符串,解析过程如下:
1、调用解析:
public ActionResult GetEntityFromXml()
{
string str = @"<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[帐号余额不足,请到商户平台充值后再重试]]></return_msg>
<result_code><![CDATA[FAIL]]></result_code>
<err_code><![CDATA[NOTENOUGH]]></err_code>
<err_code_des><![CDATA[帐号余额不足,请到商户平台充值后再重试]]></err_code_des>
<mch_billno><![CDATA[20170114130807a1234567]]></mch_billno>
<mch_id><![CDATA[**********]]></mch_id>
<wxappid><![CDATA[wx****************]]></wxappid>
<re_openid><![CDATA[oPg_***********************]]></re_openid>
<total_amount>100</total_amount>
</xml>";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(str);
var rmodel = new WeiXinRedPackgeResultModel();
Html_Tools.Class1.GetEntityFromXml<WeiXinRedPackgeResultModel>(rmodel, doc); return Json(rmodel);
}
2、解析类(可完善通用):
public static class Class1
{
public static void GetEntityFromXml<T>(this T entity, System.Xml.XmlDocument doc)
{
Dictionary<string, string> Dic = new Dictionary<string, string>();
System.Xml.XmlNodeList nodelist = doc.DocumentElement.ChildNodes;
if (nodelist.Count > )
{
Type t = entity.GetType();
foreach (System.Xml.XmlElement el in nodelist)//读元素值
{
System.Reflection.PropertyInfo proInfo = t.GetProperty(el.Name);//访问Obj的类型的属性Value的属性信息(propertyInfo)
if (proInfo != null)
{
string str2 = proInfo.PropertyType.Name;
if (str2 == null)
{
goto Label_018A;
}
if (!(str2 == "DateTime"))
{
if (str2 == "Boolean")
{
goto Label_00E9;
}
if (str2 == "Int64")
{
goto Label_0134;
}
goto Label_018A;
}
proInfo.SetValue(entity, new DateTime(long.Parse(el.InnerText)), null);
}
continue;
Label_00E9:
proInfo.SetValue(entity, el.InnerText == "", null);
continue;
Label_0134:
proInfo.SetValue(entity, long.Parse(el.InnerText), null);
continue;
Label_018A:
proInfo.SetValue(entity, el.InnerText, null);
}
}
} }
3、返回的实体类:
public class WeiXinRedPackgeResultModel
{
#region 微信红包返回值 public string return_code { get; set; } public string return_msg { get; set; } public string result_code { get; set; } public string err_code { get; set; } public string err_code_des { get; set; }
public string mch_billno { get; set; }
public string mch_id { get; set; }
public string wxappid { get; set; }
public string re_openid { get; set; } #endregion
}
XML 字符串解析的更多相关文章
- js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题
最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...
- XML字符串解析实体类方法
/// <summary> /// XML字符串解析实体类方法 /// </summary> public class StringXML { public StringXML ...
- xml 字符串解析成通用的map
[quote]Stax解析技术:快速高效,根据流的形式解析xml,比dom解析技术更加快,dom解析技术是基于文档结构树的,会把整个dom文件树加载到内存进行解析[/quote] package co ...
- XML字符串解析
不多说,直接上代码: import java.io.StringReader; import org.dom4j.Document; import org.dom4j.DocumentExceptio ...
- XML字符串解析成对象的时候应注意空格
BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头
- [ java 工具类] xml字符串解析成Map(DOM解析)
package com.tencent.jungle.wechat.util; import com.google.inject.Singleton; import org.w3c.dom.Docum ...
- dom4j组装xml 以及解析xml
dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...
- JS解析XML文件和XML字符串
JS解析XML文件 <script type='text/javascript'> loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览 ...
- tinyXml直接解析XML字符串
一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串. XML文件: <?xml version=\"1.0\" encoding=\& ...
随机推荐
- tcp 的6个控制位
原文:http://blog.chinaunix.net/uid-26413668-id-3376762.html TCP(Transmission Control Protocol) 传输控制协议 ...
- new关键字的理解-问题型
//输出的结果是?????????//The answer is good and gbc public class Example { String str=new String("goo ...
- They Are Everywhere
They Are Everywhere Sergei B., the young coach of Pokemons, has found the big house which consists o ...
- 为图片存储而作——记一次UEditor源码的修改
本文版权归博客园和作者吴双本人共同所有. 写在前面 这是一个数据爆发的网络时代,大家习惯于浏览图文直观带给我们的快速信息.大图片的存储和浏览经常会成为Web服务器的瓶颈.试想如果你的Web服务器依然 ...
- 利用html5压缩图片,产出base64图片
/* 将页面选择的图片等比压缩成指定大小(长边固定) file:图片文件 callBack:回调函数 maxLen:长边的长度*/function makePic(file,callBack,maxL ...
- 使用Jetty搭建Java Websocket Server,实现图像传输
https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...
- Struts2--模块包含
login.xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUB ...
- 计算机学院大学生程序设计竞赛(2015’12) 1009 The Magic Tower
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using ...
- 计算机学院大学生程序设计竞赛(2015’12) 1003 The collector’s puzzle
#include<cstdio> #include<algorithm> using namespace std; using namespace std; +; int a[ ...
- POJ3352 Road Construction 双连通分量+缩点
Road Construction Description It's almost summer time, and that means that it's almost summer constr ...