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的更多相关文章

  1. xml转json和实体类的两种方式

    本文为博主原创,未经允许不得转载: xml在http通信中具有较高的安全性和传输速度,所以应用比较广泛, 在项目中往往需要对xml,json和实体类进行相互转换,在这里总结一下自己所用到的一些方法: ...

  2. C#序列化s实体类成Xml,去除空格、换行符以及命名空间

    序列化实体类成为一个干净的Xml,不带空格.换行符以及命名空间 /// <summary> /// 序列化成XML /// </summary> /// <typepar ...

  3. ibatis的xml中resultmap是实体类与查询结果的一个映射

    resultmap可以少于实体类的属性,但是resultmap中的映射列,必须在查询结果中有

  4. Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类

    1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...

  5. 复杂xml格式报文和实体类之间的转化

    pom.xml中引入如下依赖: <dependency> <groupId>org.eclipse.persistence</groupId> <artifa ...

  6. 使用C#实现实体类和XML相互转换

    一.实体类转换成XML 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T& ...

  7. XML文件与实体类的互相转换

    XML文件与实体类的互相转换 一.将XML文件反序列化为实体类对象 1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config).但是现在为了演示XML ...

  8. C#实体类(复杂类)与XML互相转换

    实体类转换成XML方法: 将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化 public static string XmlSerialize<T ...

  9. .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类

    在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...

随机推荐

  1. (50)与magento集成

    我对接的是 odoo8 和 magento1.9.x 准备工作: l  服务器 装上mangento 组件 : $  pip install magento 装上 requests 组件:$ pip ...

  2. python 正则表达式与re模块

    一.正则表达式 用途 用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. #### 简单地说 就是用于字符串匹配的 字符组 在 ...

  3. 2014 百度之星 1003 题解 Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  4. 怎样在Ubuntu手机平台中开发Cordova HTML5应用

    我们知道Cordova HTML5应用具有夸平台的特性,同一时候也具有訪问本地一些资源的能力.在今天的这篇文章中.我们将介绍一下怎样创建并执行一个Cordova HTML5的应用到我们的Ubuntu手 ...

  5. github如何多人开发一个项目

    github如何多人开发一个项目 一.总结 一句话总结:a.点项目里面的Settings->Collaborators,来添加参与者(比如github用户名), b.向他发送项目的link,让他 ...

  6. 85.Mongoose指南 - Schema

    转自:https://www.bbsmax.com/A/pRdBnKpPdn/ 定义schema 用mongoose的第一件事情就应该是定义schema. schema是什么呢? 它类似于关系数据库的 ...

  7. [Codeforces 757E] Bash Plays with Functions (数论)

    题目链接: http://codeforces.com/contest/757/problem/E?csrf_token=f6c272cce871728ac1c239c34006ae90 题目: 题解 ...

  8. Objects and values

    If we execute these assignment statements: We know that a and b both refer to a string, but we don’t ...

  9. CMake入门之创建一个基于PCL的最小工程

    最近在学习PCL,借助Cmake可省去繁琐的添加包含目录和依赖库操作. 一个典型的CMakeLists.txt内容通常为: cmake_minimum_required(VERSION 2.6 FAT ...

  10. codeforces 495D Sonya and Matrix

    Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Son ...