微信红包发送完成后返回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 字符串解析的更多相关文章

  1. js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题

    最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...

  2. XML字符串解析实体类方法

    /// <summary> /// XML字符串解析实体类方法 /// </summary> public class StringXML { public StringXML ...

  3. xml 字符串解析成通用的map

    [quote]Stax解析技术:快速高效,根据流的形式解析xml,比dom解析技术更加快,dom解析技术是基于文档结构树的,会把整个dom文件树加载到内存进行解析[/quote] package co ...

  4. XML字符串解析

    不多说,直接上代码: import java.io.StringReader; import org.dom4j.Document; import org.dom4j.DocumentExceptio ...

  5. XML字符串解析成对象的时候应注意空格

    BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头

  6. [ java 工具类] xml字符串解析成Map(DOM解析)

    package com.tencent.jungle.wechat.util; import com.google.inject.Singleton; import org.w3c.dom.Docum ...

  7. dom4j组装xml 以及解析xml

    dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...

  8. JS解析XML文件和XML字符串

    JS解析XML文件 <script type='text/javascript'> loadXML = function(xmlFile){ var xmlDoc=null; //判断浏览 ...

  9. tinyXml直接解析XML字符串

    一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串. XML文件: <?xml version=\"1.0\" encoding=\& ...

随机推荐

  1. maven项目如何跳过某些junit test,或者指定执行部分junit test

    maven-surefire-plugin的插件提供了对测试目录的配置,想要细看的或者学习更多东西的可以去http://maven.apache.org/surefire/maven-surefire ...

  2. QWidget QMainWindow QDialog 三个基类的区别

    Qt类是一个提供所需的像全局变量一样的大量不同的标识符的命名空间.通常情况下,你可以忽略这个类.QObject和一些其它类继承了它,所以在这个Qt命名空间中定义的所有标识符通常情况下都可以无限制的使用 ...

  3. (转)windows上virtualenv 安装及使用

    [注意]要在某个含有空格的目录下面创建virtualenv环境,就要安装 win32api . 原文地址:http://blog.csdn.net/liuchunming033/article/det ...

  4. openwrt串口的使用

    从 RT5350 的芯片手册上可以得知, RT5350 一共有两个串口, 分别为 UART Lite. UART Full, UART Lite 就是我们惯称为的串口 1,作为系统调试串口,通过这个串 ...

  5. hrbustoj 2283 heap解题报告

    这是我们校赛的一道题,给一个字符串,判断这是字符串描绘的是不是一个堆,并不难,只是一个简单的模拟,但是也稍微有点麻烦,最起码我的方法代码量比较大,主要用栈做一个父亲与儿子的位置匹配,匹配的方法应该有很 ...

  6. Unity3d疑难问题解决

    1. 加载 c++ dll 不成功,报这个错: Failed to load 'Assets/Plugins/x86_64/myFile.dll' with error 'The operation ...

  7. android里uri和url的区别

    URI :是从虚拟根路径开始的 URI,是uniform resource identifier URL:是整个链接  URI,是uniform resource location uri:file: ...

  8. oracle创建游标和输出

    declare  cursor c_test_tbl2 is    select t2.id, t2.salary      from test_tbl2 t2, test_tbl1 t1     w ...

  9. hibernate--student_course_score

    学生, 课程,分数的设计. a)使用联合主键@EmbeddedId 使用Serializable接口 b)不适用联合主键 联合主键: 3张表, student: id, name course: id ...

  10. 对于形式参数只能用final修饰符,其它任何修饰符都会引起编译器错误

    在Java中修饰符总共有一下几种: 1.访问控制修饰符    分别有:public private protected,缺省 2.其它修饰符      分别有:abstract,final,stati ...