xml映射为object对象,同时object对象,以xml来表示:

    public class Tools
{
private static XmlNodeList SelectNodes(string xpath, string path)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
return doc.SelectNodes(xpath);
}
private static XmlNode SelectSingleNode(string xpath, string path)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
return doc.SelectSingleNode(xpath);
} public static T SelectSingle<T>(string selectNode, string path) where T : IxmlToObject<T>, new()
{
T item = new T(); var node = SelectSingleNode(selectNode, path); if (node != null)
{
T obj = item.XmlToObject(node); }
return item;
}
public static void SaveXml(IxmlFormat obj, string path)
{
XmlDocument doc = new XmlDocument();
var xml = obj.ToXml();
doc.LoadXml(xml);
doc.Save(path);
} public static List<T> SelectList<T>(string selectNode,string path) where T:new()
{
List<T> items = new List<T>(); var nodes = SelectNodes(selectNode,path); if (nodes != null)
{
var type = typeof(T);
var properties = type.GetProperties().ToList(); foreach (XmlNode node in nodes)
{
T config = new T(); foreach (XmlAttribute a in node.Attributes)
{
string name = a.Name;
string value = a.Value; var p = properties.FirstOrDefault(t => t.Name.ToLower() == name.ToLower()); if (p != null)
{
p.SetValue(config, value, null);
}
}
items.Add(config);
}
}
return items;
} public static string ReplaceSpecialChars(string content)
{
if (string.IsNullOrEmpty(content)) return ""; string[] specialChars = { "&", "<", ">", "\"", "'" };
string[] entities = { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" }; int i = ;
foreach (var item in specialChars)
{
content = content.Replace(item, entities[i]);
i++;
}
return content;
}
}

这是公共的接口:

    public interface IxmlFormat
{
string ToXml();
}
public interface IxmlToObject<T>
{
T XmlToObject(XmlNode node);
}

下面是自定义Object对象对接口的实现:

public class TestCase : IxmlFormat, IxmlToObject<TestCase>
{
public string Title { set; get; }
public string Author { set; get; }
public string BibType { set; get; }
/// <summary>
/// 测试用例路径
/// </summary>
public string FilePath { set; get; } public List<SearchResult> StandardResults { set; get; } public List<SearchResult> SearchResults { set; get; } public string ToXml()
{
var title = Tools.ReplaceSpecialChars(this.Title);
var author = Tools.ReplaceSpecialChars(this.Author);
var bibType = Tools.ReplaceSpecialChars(this.BibType); StringBuilder sb = new StringBuilder(); sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"); if (SearchResults != null && SearchResults.Count > )
{
sb.AppendFormat("<case title=\"{0}\" author=\"{1}\" bibType=\"{2}\" >", title, author, bibType); foreach (SearchResult item in SearchResults)
{
sb.AppendFormat("<item title=\"{0}\">", Tools.ReplaceSpecialChars(item.Title)); foreach (var fitem in item.Fields)
{
sb.AppendFormat("<field content=\"{0}\"/>", Tools.ReplaceSpecialChars(fitem));
}
sb.AppendFormat("</item>");
} sb.Append("</case>");
}
return sb.ToString();
} public TestCase XmlToObject(XmlNode node)
{
string title = node.Attributes["title"].Value;
string author = node.Attributes["author"].Value;
string bibType = node.Attributes["bibType"].Value; this.Title = title;
this.Author = author;
this.BibType = bibType; this.StandardResults = new List<SearchResult>();
XmlNodeList itemNodes = node.SelectNodes("//item");
foreach (XmlNode n in itemNodes)
{
var sr = new SearchResult()
{
Title = n.Attributes["title"].Value,
Fields = new List<string>()
}; var fileds = n.ChildNodes; foreach (XmlNode fn in fileds)
{
sr.Fields.Add(fn.Attributes["content"].Value);
}
this.StandardResults.Add(sr);
} return this;
}
} public class SearchResult
{
public string Title { set; get; } public List<string> Fields { set; get; } }

这是一个测试用例,程序从xml文件中读取测试用例,运行测试程序,完成后把结果保存到另外一个xml文件中,这个xml文件结构和测试用例的xml结构一样。我们看看如何读取测试用例:

        /// <summary>
/// 读取测试用例
/// </summary>
/// <returns></returns>
private static List<TestCase> GetTestCase(string caseDir)
{
List<TestCase> tests = new List<TestCase>(); var files = Directory.GetFiles(caseDir); foreach (var f in files)
{
var filePath = f;
var testCase = Tools.SelectSingle<TestCase>("//case", filePath);
testCase.FilePath = filePath;
tests.Add(testCase);
} return tests;
}

保存测试结果:

         TestCase t = new TestCase()
{
FilePath = viewModel.FilePath,
Author = viewModel.SearchAuthor,
Title = viewModel.SearchTitle,
BibType = viewModel.SearchType,
SearchResults = viewModel.TestResultData,
StandardResults = viewModel.StandardResults
}; string path = viewModel.TestResultFilePath + "\\" + viewModel.ResolveName; if (!Directory.Exists(path)) Directory.CreateDirectory(path); path += "\\" + Path.GetFileNameWithoutExtension(t.FilePath) + "_data.xml"; Tools.SaveXml(t, path);

注意:对象与xml之间的转换,再转为xml时,注意特殊符号的转义,否则会报错。

xml与object 之间的ORM的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用spring framework的IoC容器功能----->方法一:使用XML文件定义beans之间的依赖注入关系

    XML-based configuration metadata(使用XML文件定义beans之间的依赖注入关系) 第一部分 编程思路概述 step1,在XML文件中定义各个bean之间的依赖关系. ...

  2. Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM)

    Django中ORM介绍和字段及字段参数   Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简 ...

  3. Object Relational Mapping(ORM)

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  4. RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject

    catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...

  5. JAXB 操作XML 与 Object

    Java Architecture for XML Binding) 是一个业界的标准,是一项能够依据XML Schema产生Java类的技术.是一种xml与object映射绑定技术标准. JDK5下 ...

  6. php中 xml json 数组 之间相互转换

    php中 xml json  数组 之间相互转换 1 数组转json $result = array( 'status' =>$status, 'message'=>$message, ' ...

  7. applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found

    applicationContext.xml报错org.springframework.orm.hibernate3.LocalSessionFactoryBean not found 解决办法: 1 ...

  8. java中Xml、json之间的相互转换

    旁白: 最近关于xml与json之间的转换都搞蒙了,这里写一个demo,以后备用. 正题: project格式是: jar包是一个一个检出来的,还算干净了. 代码: 工具类: package exer ...

  9. org.json里实现XML和JSON之间对象互转

    org.json包里有一个类org.json.XML可以实现XML和JSON之间的转换.http://www.json.org/javadoc/org/json/XML.html JSONObject ...

随机推荐

  1. Java基础---Java---IO流-----File 类、递归、删除一个带内容的目录、列出指定目录下文件夹、FilenameFilte

    File 类 用来将文件或者文件夹封装成对象 方便对文件与文件夹进行操作. File对象可以作为参数传递给流的构造函数 流只用操作数据,而封装数据的文件只能用File类 File类常见方法: 1.创建 ...

  2. UNIX网络编程——带外数据小结

    TCP没有真正的带外数据,不过提供紧急模式和紧急指针.一旦发送端进入紧急模式,紧急指针就出现在发送到对端的分节中的TCP首部中.连接的对端收取该指针是在告知接收进程发送端已经进入紧急模式,而且该指针指 ...

  3. EBS密码加密研究

     DECLARE   v_password_1 VARCHAR2(240);   v_password_2 VARCHAR2(240);   v_password_3 VARCHAR2(240); ...

  4. Android回调事件传播-android学习之旅(四十五)

    概念简介 代码演示 package peng.liu.test; import android.app.ActionBar; import android.app.Activity; import a ...

  5. Google Guava的5个鲜为人知的特性

    译文出处: 花名有孚   原文出处:takipi.com Google Guava有哪些比较冷门但却又实用的特性呢? 它是最流行的开源库之一,你应该听过它的大名,它诞生的地方正是人们举办真正的魁地奇比 ...

  6. Android5.1设备无法识别exFAT文件系统的64G TF卡问题

    64G TF卡刚买回来的时候默认exFAT文件系统,在电脑端(XP和WIN7)可以识别,但在我们Android5.1S设备无法识别,采用guiformat工具格式化为FAT32文件系统后才可以正常识别 ...

  7. driver: Linux设备模型之input子系统详解

    本节从整体上讲解了输入子系统的框架结构.有助于读者从整体上认识linux的输入子系统.在陷入代码分析的过程中,通过本节的知识能够找准方向,明白原理. 本节重点: 输入子系统的框架结构 各层对应内核中的 ...

  8. 开源摄影机:Axiom Camera

    一般情况下只有软件才有开源这个概念.这会儿发现了个很厉害的开源的产品:开源摄影机. 我还是第一次听说摄影机也可以开源.于是去该产品的官方网站了解了一下相关信息. 官网:http://axiom.ape ...

  9. EventBus的其他常用函数

    上一篇EventBus最简易使用方式介绍了EventBus最简易的使用方式,摆脱了叽里呱啦+图片的长篇大论.目的是为了让刚开始接触的人们不晕头转向.那么这篇..我也要开始图片+叽里呱啦了. 转载请注明 ...

  10. linux的string操作(字符串截取,长度计算)

    按指定的字符串截取 1.第一种方法: ${varible##*string} 从左向右截取最后一个string后的字符串 ${varible#*string}从左向右截取第一个string后的字符串 ...