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语句返回的实体也需要 ...
随机推荐
- android-async-http二次封装和调用
Android android-async-http二次封装和调用 在开发过程中,网络请求这块的使我们常常遇到的一个问题,今天去github 站点上面学习android-async-http,认为还 ...
- [TypeScript] Asynchronous Iteration using for-await-of
The for-await-of syntax is similar to the for-of iteration. The key difference is that it automatica ...
- ubuntu升级到14.04后终端显示重叠
系统升级后,发现这个问题非常不爽,问题不大,但有时候找不到解决方法,让人纠结好久.解决方法例如以下: 编辑->配置文件首选项->常规-> monospace 改为ubuntu mon ...
- less07 important
less .foo (@bg: #f5f5f5, @color: #900) { background: @bg; color: @color; font-size: 16px; font-weigh ...
- Metasploit的armitage初步使用
armitage的启动 root@kali:~# armitage 别急,过会儿就好了 . 等扫描完会弹出一个框框然后会多出目标的图标比如目标是打印机
- 安装meteor运行基本demo发生错误。
bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...
- [ SQLServer ] 數字類型的欄位細節 - 轉載
[MSSQL] 欄位開立(2) - decimal, numeric, float, real, money 的抉擇 https://dotblogs.com.tw/henryli/2015/06/1 ...
- Nordic Collegiate Programming Contest 2015(第七场)
A:Adjoin the Networks One day your boss explains to you that he has a bunch of computer networks tha ...
- Division Game UVA - 11859 Nim
Code: #include<cstdio> #include<algorithm> using namespace std; #define maxn 10005 int n ...
- 关于 nginx 的配置的 location
精准匹配和普通匹配: server{ location =/index.htm{ ////精准匹 ...