这个是和ALM上传测试结果结合使用的
//把xml序列化成对象以及把对象序列化成xml using System;
using System.Data;
using System.Configuration;
using System.Web; using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
namespace XMLUtils
{ /// <summary>
/// Summary description for XMLUtils
/// </summary>
public class XMLUtils
{
public XMLUtils()
{
} public static String SerializedToXML(Type ObjectType_, object ObjectInstance_)
{
return SerializedToXMLStringBuilder(ObjectType_, ObjectInstance_).ToString();
} public static StringBuilder SerializedToXMLStringBuilder(Type ObjectType_, object ObjectInstance_)
{
StringBuilder sbXML = new StringBuilder();
XmlSerializer xs = new XmlSerializer(ObjectType_);
xs.Serialize(new StringWriter(sbXML), ObjectInstance_); return sbXML;
} public static object Deserialized(Type ObjectType_, string SerializedText_)
{
XmlSerializer xs = new XmlSerializer(ObjectType_);
object oDeserialized = xs.Deserialize(new StringReader(SerializedText_)); return oDeserialized;
} /// <summary>
/// Serialize the object to stringbuilder.
/// </summary>
/// <param name="obj"></param>
/// <param name="targetNamespace"></param>
/// <returns></returns>
public static StringBuilder SerializeObject(object obj, string targetNamespace)
{
System.Xml.Serialization.XmlSerializer ser;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
if (string.IsNullOrEmpty(targetNamespace))
{
ns.Add(string.Empty, string.Empty);
}
else
{
ns.Add("ns", targetNamespace);
} ser = new System.Xml.Serialization.XmlSerializer(obj.GetType());
StringBuilder objectAsString = new StringBuilder();
StringWriter stringWriter = new StringWriter(objectAsString);
XmlTextWriter writer = new XmlTextWriter(stringWriter);
writer.Formatting = Formatting.None;
ser.Serialize(writer, obj, ns);
return objectAsString;
} /// <summary>
/// Deserialize the stringbuilder to object.
/// </summary>
/// <param name="objectType"></param>
/// <param name="serializedXML"></param>
/// <returns></returns>
public static object DeserializeXML(Type objectType, StringBuilder serializedXML)
{
System.IO.StringReader reader = new System.IO.StringReader(serializedXML.ToString());
object deserializedObject = null;
try
{
XmlSerializer xs = new XmlSerializer(objectType);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(reader);
deserializedObject = xs.Deserialize(xr); }
catch (Exception) { }
finally
{
if (reader != null)
{
reader.Dispose();
}
}
return deserializedObject;
}
}
}

XML序列化成对象的更多相关文章

  1. form表单元素的值序列化成对象

    /** * 将form表单元素的值序列化成对象 * param: form jquery form对象 */ var serializeObject = function(form) { var o ...

  2. 将form表单元素的值序列化成对象

    /**jQuery * 将form表单元素的值序列化成对象 * @returns object */ var serializeObject = function(form) { var o = {} ...

  3. XML 反序列化成对象,绑定到CheckBoxList控件

    1.前台 <div class="control-group"> <label class="control-label"> 导航名称: ...

  4. jquery easyui将form表单元素的值序列化成对象

    function serializeObject(form){ var o={}; $.each(form.serializeArray(),function(index){ if(o[this['n ...

  5. C# .NET XML 序列化为对象,反序列化

    如果遇到:   根级别上的数据无效. 行 1,位置 1   .:即无法反序列化(反序列失败),得到对象为null ,把 xml 文本 Trim一下. xml=xml.Trim(); 序列化完毕你可以看 ...

  6. C#对象序列化成XML,以及自定义标签名

    C#对象序列化操作: public class XMLHelper { /// <summary> /// 对象序列化成 XML String /// </summary> p ...

  7. 将Java对象序列化成JSON和XML格式

    1.先定义一个Java对象Person: public class Person { String name; int age; int number; public String getName() ...

  8. C#将对象序列化成JSON字符串

    C#将对象序列化成JSON字符串 public string GetJsonString() { List<Product> products = new List<Product& ...

  9. IOS开发之把 Array 和 Dictionaries 序列化成 JSON 对象

    1 前言通过 NSJSONSerialization 这个类的 dataWithJSONObject:options:error:方法来实现,Array 和 dictionary 序列化成 JSON ...

随机推荐

  1. CentOS下判断自己的VPS是OpenVZ的还是Xen的

    一般来说,VPS的虚拟化技术,有Xen.OpenVZ.Xen HVM和VMware这几种,那么,如何判断自己的VPS是基于哪种虚拟化技术的呢? 1.执行:ls /proc/命令,一般Xen的VPS,/ ...

  2. nodpad++正则替换

    则表达式是一个查询的字符串,它包含一般的字符和一些特殊的字符,特殊字符可以扩展查找字符串的能力,正则表达式在查找和替换字符串的作用不可忽视,它 能很好提高工作效率. EditPlus的查找,替换,文件 ...

  3. ES6入门之Generator函数

    Generator Generator函数是ES6提供的一种异步编程解决方案,Generator函数是一个状态机,封装了多个内部状态. 执行Generator函数会返回一个遍历器对象,也就是说,Gen ...

  4. USACO Section 3.1: Score Inflation

    完全背包问题 /* ID: yingzho1 LANG: C++ TASK: inflate */ #include <iostream> #include <fstream> ...

  5. Crypto API加密通信流程

    应用程序使用Crypto API进行加密通信的一般步骤如下: 1,include wincrypt.h 2,调用CryptAcquireContext()获得某个CSP模块中的密钥容器(key con ...

  6. 《大道至简-Team》

    已经学习了<大道至简>两章,我们了解了编程的本质和“懒人”造就了方法.书中没有提供给我们编程的技巧,捷径,而是从别的方面为我们讲解了编程的精义.第三章就为我们引入了“团队”这个概念. 我们 ...

  7. 在tomcat目录下启动tomcat,可以正常访问tomcat主页,然在在eclipse中集成了tomcat却访问不了tomcat主页,却能访问发布的项目

    tomcat server在eclipse中正常配置了,在eclipse建tomcat服务是在server 视图那里new server建立的,但把项目部署到tomcat后却发现tomcat主页报40 ...

  8. hdu2847(暴力)

    去年看的一道题目,但是竟然傻傻的用dfs+循环链表去做. 简直傻到爆.  不过现在做这题还是想了好久而且还有好几次WA,其实这题还是很水的.直接暴力枚举就行了,枚举的前提是要算好复杂度, 可以知道的是 ...

  9. c#调用系统资源大集合-1

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  10. staging server, source congtrol, deply workflow using git

    web项目开发中,有三个实践对于项目成功是非常重要的: 1. staging servers 2. Version control workflows 3. Tested, repeatable de ...