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语句返回的实体也需要 ...
随机推荐
- Python: Json串反序列化为自定义类对象
最近刚接触到python,就想到了如何反序列化json串.网上找了一下,大部分都是用json模块反序列化为python数据结构(字典和列表).如果对json模块不了解的参考菜鸟教程.然后我在此基础上将 ...
- docker mysql 文件挂载和MySQL字符集设置
原文:docker mysql 文件挂载和MySQL字符集设置 docker run -p 3306:3306 --name mysql -v /usr/local/mysql/my.cnf:/etc ...
- Python+Appium来写app自动化脚本
1...........................我有空再补
- 机载LIDAR技术及其应用
1 机载LIDAR的系统组成及原理 1.1 机载 LIDAR 技术的发展历程 LIDAR 技术和机载激光扫描技术的发展源自 1970 年,美国航空航天局(NASA)支持研制成功第一台对地观测 LIDA ...
- 洛谷——P2661 信息传递
https://www.luogu.org/problem/show?pid=2661#sub 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其 ...
- windows 下面的grep awk 命令
windows 下面的grep awk 命令 grep 学习了:http://blog.csdn.net/chengfans/article/details/53784936 awk学习了:http: ...
- [Angular] Configure an Angular App at Compile Time with the Angular CLI
Compile time configuration options allow you to provide different kind of settings based on the envi ...
- LeetCode 之 Merge Sorted Array(排序)
[问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- mfc 链接 access 2007 数据库
神马也不说了,直接给出源代码和project 原理这个东西 Google 下.都出来了.自己就说下作为新手 , 1 应该打印出,链接错误原因 2 应该将数据库放到project以下,特别注意这点 给 ...
- Sql中把datetime转换成字符串(CONVERT)
一.回想一下CONVERT()的语法格式: CONVERT (<data_ type>[ length ], <expression> [, style]) 二.这里注重说明一 ...