这篇文章主要为大家详细介绍了c#实现实体类和xml相互转换的资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
一、实体类转换成xml
将实体类转换成xml需要使用xmlserializer类的serialize方法,将实体类序列化
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();
}
}
示例:
1、定义实体类
[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; }
}
2、给实体类赋值,并通过序列化将实体类转换成xml格式的字符串
request patientin = new request();
patientin.system = "his";
patientin.securitycode = "his5";
patientbasicinfo basicinfo = new patientbasicinfo();
basicinfo.patientno = "1234";
basicinfo.patientname = "测试";
basicinfo.phoneticize = "";
basicinfo.sex = "1";
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;
//序列化
string strxml = xmlserializehelper.xmlserialize<request>(patientin);
3、生成的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>1234</patientno>
<patientname>测试</patientname>
<phoneticize />
<sex>1</sex>
<birth />
<birthplace />
<country />
<nation />
<idnumber />
<securityno />
<workunits />
<address />
<zipcode />
<phone />
<contactperson />
<contactship />
<contactpersonadd />
<contactpersonphone />
<operationcode />
<operationname />
<operationtime />
<cardno />
<changetype />
</patientinfo>
</request>
二、将xml转换成实体类
把xml转换成相应的实体类,需要使用到xmlserializer类的deserialize方法,将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;
}
}
示例:
将上例中序列化后的xml反序列化成实体类
//反序列化
request r = xmlserializehelper.deserializer<request>(strxml);
以上就是c#实现实体类和xml相互转换的示例代码详解的详细内容。