欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现实体类和XML相互转换

程序员文章站 2023-11-18 21:10:04
一、实体类转换成xml 将实体类转换成xml需要使用xmlserializer类的serialize方法,将实体类序列化 public static strin...

一、实体类转换成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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。