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语句返回的实体也需要 ...
随机推荐
- 【转】 C#学习笔记14——Trace、Debug和TraceSource的使用以及日志设计
[转] C#学习笔记14——Trace.Debug和TraceSource的使用以及日志设计 Trace.Debug和TraceSource的使用以及日志设计 .NET Framework 命名空 ...
- exadata(硬件更换文档部分)
Maintaining Flash Disks Replacing a Flash Disk Due to Flash Disk Failure Each Exadata Storage Server ...
- jsp urlrewrite 中正則表達式不包括某个字符串写法
因在程序中须要做城市间跳转,可是页面中包括的css.scripts和图片等路径是要排除在外的. 这就须要在正则中指定当遇到哪些 字符时须要略过. 正则例如以下: /((? !css)(?!script ...
- 教你怎样做个有“钱”途的測试project师
百度百科说測试project师这一职业的待遇,薪酬上升空间很大.但測试project师也有自己的烦恼,比方在程序出错后,将问题反馈给程序猿,然后程序猿给的答复是:"oh,howisthatp ...
- Android之Http通信——1.初识Http协议
Android之Http通信--1.初识Http协议 引言: 今天是六一儿童节,先在这里给各位超龄儿童说声节日快乐哈~( ╯□╰ ),小猪也象征性地给群里的小朋友们派了红包-嗯,忙碌的五月最终过去了, ...
- 关于Android真机调測Profiler
u3d中的Profile也是能够直接在链接安卓设备执行游戏下查看的,导出真机链接U3D的Profile看数据,这样能更好的測试详细原因. 大概看了下官方的做法.看了几张帖子顺带把做法记录下来. 參考: ...
- Could not load the FreeMarker template named 'select'
眼下项目使用struts2, 所以页面中就使用到了struts2的标签,可是今天在做新的功能的时候突然出现 "Could not load the FreeMarker template n ...
- PostgreSQL Replication之第一章 理解复制概念(2)
1.2不同类型的复制 现在,您已经完全地理解了物理和理论的局限性,可以开始学习不同类型的复制了. 1.2.1 同步和异步复制 我们可以做的第一个区分是同步复制和异步复制的区别. 这是什么意思呢?假设我 ...
- UVa 1600 Patrol Robot【BFS】
题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...
- 新人 记录VUE中分页实现
关于函数传值 this.getPurchaseHistoryData(index, num,timeType);第一位是显示的页数,第二是控制首页4上一页-1下一页是2末页是5 第三是是对昨天是1,今 ...