C#中实体集合和实体转换成相应的string、XDocument、XElement

public class SimpleXmlConverter
{
public static string ToXml<T>(IList<T> entities, string rootName = "") where T : new()
{
if (entities == null || entities.Count == )
{
return string.Empty;
} StringBuilder builder = new StringBuilder();
builder.AppendLine(XmlResource.XmlHeader); XElement element = ToXElement<T>(entities, rootName);
builder.Append(element.ToString()); return builder.ToString();
} public static XmlDocument ToXmlDocument<T>(IList<T> entities, string rootName = "") where T : new()
{
if (entities == null || entities.Count == )
{
return null;
} XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(ToXml<T>(entities, rootName)); return xmlDocument;
} public static XDocument ToXDocument<T>(IList<T> entities, string rootName = "") where T : new()
{
if (entities == null || entities.Count == )
{
return null;
} return XDocument.Parse(ToXml<T>(entities, rootName));
} public static XElement ToXElement<T>(IList<T> entities, string rootName = "") where T : new()
{
if (entities == null || entities.Count == )
{
return null;
} if (string.IsNullOrWhiteSpace(rootName))
{
rootName = typeof(T).Name + XmlResource.XmlRootNameSuffix;
} XElement element = new XElement(rootName); foreach (T entity in entities)
{
element.Add(ToXElement<T>(entity));
} return element;
} public static string ToXml<T>(T entity) where T : new()
{
if (entity == null)
{
return string.Empty;
} XElement element = ToXElement<T>(entity); return element.ToString();
} public static XElement ToXElement<T>(T entity) where T : new()
{
if (entity == null)
{
return null;
} XElement element = new XElement(typeof(T).Name);
PropertyInfo[] properties = typeof(T).GetProperties();
XElement innerElement = null;
object propertyValue = null; foreach (PropertyInfo property in properties)
{
propertyValue = property.GetValue(entity, null);
innerElement = new XElement(property.Name, propertyValue);
element.Add(innerElement);
} return element;
} public static XElement ToXElement(Type type)
{
if (type == null)
{
return null;
} XElement element = new XElement(type.Name);
PropertyInfo[] properties = type.GetProperties();
XElement innerElement = null; foreach (PropertyInfo property in properties)
{
innerElement = new XElement(property.Name, null);
element.Add(innerElement);
} return element;
}
}

C#中实体集合和实体转换成相应的string、XDocument、XElement的更多相关文章

  1. C# 实体集合和实体转换成相应的string、XDocument、XElement、XDocument

    https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement(v=vs.110).aspx XElement.Parse 方法 ( ...

  2. 将泛类型集合List类转换成DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  3. ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)

    原文:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100) 对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是 ...

  4. C#将image中的显示的图片转换成二进制

    原文:C#将image中的显示的图片转换成二进制 1.将Image图像文件存入到数据库中 我们知道数据库里的Image类型的数据是"二进制数据",因此必须将图像文件转换成字节数组才 ...

  5. 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功

    public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...

  6. Java中Office(word/ppt/excel)转换成HTML实现

    运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...

  7. [转]C#将image中的显示的图片转换成二进制

    本文转自:http://www.cnblogs.com/shuang121/archive/2012/07/09/2582654.html .将Image图像文件存入到数据库中 我们知道数据库里的Im ...

  8. 转:ArcGIS中利用ArcMap将地理坐标系转换成投影坐标系(从WKID=4326到WKID=102100)

    对于非地理专业的开发人员,对与这些生涩的概念,我们不一定都要了解,但是我们要理解,凡是以经纬度为单位的都是地理坐标系,因为它归根结底是一个椭球体,只不过各个国家为了反映该国家所在区域地球的真实形状,而 ...

  9. python3.6 + selenium2.53.1 查询数据库并将返回的内容中每一行的内容转换成class对象

    环境: win10 python3.6 selenium2.53.1 准备工作:先安装pymysql python2.x链接数据库使用MySQLdb,而python3.x链接数据库使用pymysql ...

随机推荐

  1. 第一个HelloWorld!

    $.介绍 1.eclipse的基本使用 2.第一个程序HelloWorld 3.总结 $.基本使用 对于刚入门的java新手来说选择一个舒适的编译器能让你快速的上手java的程序编写. 针对英语low ...

  2. ubuntu17.04安装flash

    因为用不了软件商店(别问我为什么) 所以手动安装 1 下载文件 在firefox下下载  *****.tar.gz 压缩包 ,并解压(一般目录在 /home 当前用户下的 下载目录下) adobe官网 ...

  3. 自定义等高 Cell

    1.介绍 1.1 代码自定义 cell(使用 frame) 创建一个继承自 UITableViewCell 的子类,比如 BookCell1. 在 initWithStyle:reuseIdentif ...

  4. iframe相关操作

    内容摘要 父级获取iframe中的元素 父级触发iframe中的函数 iframe触发父级元素绑定的事件 iframe触发父级函数方法 iframe触发父级元素的值 parent.html <! ...

  5. b,u,i,s,这些被删除的标签以及用来替换他们的标签

    加粗文字 给文字加下划线 倾斜文字 给文字加删除线 这些是以前的HTML语言修饰文字用的,没有语义,所以被删除,不到万不得已 不能使用,HTML给了新的标签替换他们,并且有语义 定义重要性强调的文字 ...

  6. 【三支火把】--- 关于UEFI&PCD的总结介绍

    1个人理解 个人理解PCD基本等同于Token,应当算是一种描述性语言,按照规定书写好PCD的配置档,在编译的过程中,会根据你的配置生成同等含义的C文档,而在C文档中对应会出现相应的define或者变 ...

  7. [BOI2007]Sequence 序列问题 BZOJ1345

    题目描述 对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一 ...

  8. [转载]sscanf函数

    来源:http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据, ...

  9. excle导出使用poi

    package com.ittax.core.util; import java.util.List; import javax.servlet.ServletOutputStream; import ...

  10. vue.js路由学习笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...