XML之序列化C#实体类,DataTable,List
1.
static void Main(string[] args)
{
#region 实体类
Request patientIn = new Request();
patientIn.System = "HIS";
patientIn.SecurityCode = "HIS5"; PatientBasicInfo basicInfo = new PatientBasicInfo();
basicInfo.PatientNo = "";
basicInfo.PatientName = "测试";
basicInfo.Phoneticize = "";
basicInfo.Sex = "";
basicInfo.Birth = "";
basicInfo.BirthPlace = "";
basicInfo.Country = "";
basicInfo.Nation = "";
basicInfo.IDNumber = "";
basicInfo.SecurityNo = "";
basicInfo.Workunits = "";
basicInfo.Address = "";
basicInfo.ZIPCode = "";
basicInfo.Phone = "";
basicInfo.ContactShip = "";
basicInfo.ContactPersonPhone = "";
basicInfo.ContactPersonAdd = "";
basicInfo.ContactPerson = "";
basicInfo.ChangeType = "";
basicInfo.CardNo = "";
basicInfo.OperationCode = "";
basicInfo.OperationName = "";
basicInfo.OperationTime = ""; patientIn.PatientInfo = basicInfo; //序列化为xml
string strxml = Class_to_xml.XmlSerialize<Request>(patientIn);
//Console.Write(strxml); //反序列化为实体类
Request r = Class_to_xml.DESerializer<Request>(strxml);
//Console.Write(r);
#endregion #region DataTable
//将DataTable转换成XML
DataTable dt = new DataTable("MyTable");
//添加列
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Sex", typeof(char));
//添加行
dt.Rows.Add(, "小明", '');
dt.Rows.Add(, "小红", '');
dt.Rows.Add(, "小王", '');
dt.Rows.Add(, "测试", '');
//序列化为xml,将DataTable转换成XML格式的字符串
string strXML = Class_to_xml.XmlSerialize<DataTable>(dt);
//Console.Write(strXML); //反序列化为DataTable
DataTable dtNew = Class_to_xml.DESerializer<DataTable>(strXML);
Console.Write(strXML);
#endregion #region 列表集合
//测试集合
List<Student> list = new List<Student>()
{
new Student(){Id=,Name="小红",Sex='',Age=},
new Student(){Id=,Name="小明",Sex='',Age=},
new Student(){Id=,Name="小王",Sex='',Age=},
new Student(){Id=,Name="测试",Sex='',Age=}
};
//序列化为xml
string strXML = Class_to_xml.XmlSerialize<List<Student>>(list);
Console.Write(strxml); //反序列化为list
List<Student> listStu = Class_to_xml.DESerializer<List<Student>>(strXML);
foreach (var item in listStu)
{
Console.WriteLine(item.Age);
}
#endregion Console.ReadKey();
}
2.
public class Class_to_xml
{
//实体类转换XML,xml序列化
public static string XmlSerialize<T>(T obj)
{
using (StringWriter sw = new StringWriter())
{
Type t = obj.GetType();
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(sw, obj);
sw.Close();
return sw.ToString();
}
}
//xml反序列化
public static T DESerializer<T>(string strXML) where T : class
{
try
{
using (StringReader sr = new StringReader(strXML))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return serializer.Deserialize(sr) as T;
}
}
catch (Exception ex)
{
return null;
}
} }
Class_to_xml
3.
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class Request
{ public string System { get; set; }
public string SecurityCode { get; set; }
public PatientBasicInfo PatientInfo { get; set; }
} /// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PatientBasicInfo
{
public string PatientNo { get; set; }
public string PatientName { get; set; }
public string Phoneticize { get; set; }
public string Sex { get; set; }
public string Birth { get; set; }
public string BirthPlace { get; set; }
public string Country { get; set; }
public string Nation { get; set; }
public string IDNumber { get; set; }
public string SecurityNo { get; set; }
public string Workunits { get; set; }
public string Address { get; set; }
public string ZIPCode { get; set; }
public string Phone { get; set; }
public string ContactPerson { get; set; }
public string ContactShip { get; set; }
public string ContactPersonAdd { get; set; }
public string ContactPersonPhone { get; set; }
public string OperationCode { get; set; }
public string OperationName { get; set; }
public string OperationTime { get; set; }
public string CardNo { get; set; }
public string ChangeType { get; set; } }
/// <summary>
/// 测试类
/// </summary>
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public char Sex { get; set; }
public int Age { get; set; }
}
实体类
4.XML
<?xml version="1.0" encoding="utf-16"?>
<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<System>HIS</System>
<SecurityCode>HIS5</SecurityCode>
<PatientInfo>
<PatientNo></PatientNo>
<PatientName>测试</PatientName>
<Phoneticize />
<Sex></Sex>
<Birth />
<BirthPlace />
<Country />
<Nation />
<IDNumber />
<SecurityNo />
<Workunits />
<Address />
<ZIPCode />
<Phone />
<ContactPerson />
<ContactShip />
<ContactPersonAdd />
<ContactPersonPhone />
<OperationCode />
<OperationName />
<OperationTime />
<CardNo />
<ChangeType />
</PatientInfo>
</Request>
序列化xml 效果
XML之序列化C#实体类,DataTable,List的更多相关文章
- xml转json和实体类的两种方式
本文为博主原创,未经允许不得转载: xml在http通信中具有较高的安全性和传输速度,所以应用比较广泛, 在项目中往往需要对xml,json和实体类进行相互转换,在这里总结一下自己所用到的一些方法: ...
- C#序列化s实体类成Xml,去除空格、换行符以及命名空间
序列化实体类成为一个干净的Xml,不带空格.换行符以及命名空间 /// <summary> /// 序列化成XML /// </summary> /// <typepar ...
- ibatis的xml中resultmap是实体类与查询结果的一个映射
resultmap可以少于实体类的属性,但是resultmap中的映射列,必须在查询结果中有
- Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类
1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...
- 复杂xml格式报文和实体类之间的转化
pom.xml中引入如下依赖: <dependency> <groupId>org.eclipse.persistence</groupId> <artifa ...
- 使用C#实现实体类和XML相互转换
一.实体类转换成XML 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T& ...
- XML文件与实体类的互相转换
XML文件与实体类的互相转换 一.将XML文件反序列化为实体类对象 1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config).但是现在为了演示XML ...
- C#实体类(复杂类)与XML互相转换
实体类转换成XML方法: 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T ...
- .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...
随机推荐
- 洛谷 P1169 [ZJOI2007]棋盘制作 (悬线法)
和玉蟾宫很像,条件改成不相等就行了. 悬线法题目 洛谷 P1169 p4147 p2701 p1387 #include<cstdio> #include<algorithm& ...
- 2015 Multi-University Training Contest 1 Assignment
Assignment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【转】C# 正则表达式大全
[转]C# 正则表达式大全 前言 在网上看到一个不错的简易版正则匹配和替换的工具,现在补充进来,感觉还不错,效果如下(输入验证中文汉字的正则表达式) 在线下载 密码:5tpt 注:好像也是一位园友 ...
- JavaScript检查手机格式是否错误
编写自己定义的JavaScript函数checkPhone(),在函数中应用正則表達式推断手机号码的格式是否正确,不对的给出提示 <script type="text/javascri ...
- leetcode 刷题之路 66 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- bzoj1934: [Shoi2007]Vote 善意的投票(显然最小割)
1934: [Shoi2007]Vote 善意的投票 题目:传送门 题解: 明显的不能再明显的最小割... st连同意的,不同意的连ed 朋友之间两两连边(即双向边) 流量都为1... 为啥: 一个人 ...
- AI目前的根本问题——缺乏 自由意志,无法分辨真正的善恶
另一位对AI表现出极大兴趣的演讲嘉宾,就是短篇科幻小说<折叠北京>的作者:郝景芳. 演讲一开始她就提到了测试AI的三个问题: 第一个测试题,如果哥伦布没有发现新大陆对中国哪个菜系影响最大? ...
- 详细图解mongodb下载、安装、配置与使用
记得在管理员模式下运行CMD,否则服务将启动失败 转载:http://blog.csdn.net/boby16/article/details/51221474 详细图解,记录 win7 64 安装m ...
- rest_framework 解析器(下 全局配置使用)
解析器 一般都是全局设置 参考文档 www.cnblogs.com/wupeiqi/articles/.html REST_FRAMEWORK=( "DEFAULT_PARSER_CLASS ...
- POJ 1631 nlogn求LIS
方法一: 二分 我们可以知道 最长上升子序列的 最后一个数的值是随序列的长度而递增的 (呃呃呃 意会意会) 然后我们就可以二分找值了(并更新) //By SiriusRen #include < ...