C 實現實體類和XML相互轉換

2021-09-19 02:57:19 字數 4712 閱讀 1014

一、實體類轉換成xml

將實體類轉換成xml需要使用xmlserializer類的serialize方法,將實體類序列化

public

static

string xmlserialize(t obj)

}

示例:1、定義實體類

[system.xml.serialization.xmltypeattribute(anonymoustype = true

)] [system.xml.serialization.xmlrootattribute(namespace = "", isnullable = false

)]

public

class

request

public

string securitycode

public patientbasicinfo patientinfo

}/// [system.xml.serialization.xmltypeattribute(anonymoustype = true

)]

public

partial

class

patientbasicinfo

public

string patientname

public

string phoneticize

public

string ***

public

string birth

public

string birthplace

public

string country

public

string nation

public

string idnumber

public

string securityno

public

string workunits

public

string address

public

string zipcode

public

string phone

public

string contactperson

public

string contactship

public

string contactpersonadd

public

string contactpersonphone

public

string operationcode

public

string operationname

public

string operationtime

public

string cardno

public

string changetype

}

2、給實體類賦值,並通過序列化將實體類轉換成xml格式的字串

request patientin = new

request();

patientin.system = "

his"

; patientin.securitycode = "

his5";

patientbasicinfo basicinfo = new

patientbasicinfo();

basicinfo.patientno = "

1234";

basicinfo.patientname = "測試"

; basicinfo.phoneticize = ""

; basicinfo.*** = "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(patientin);

3、生成的xml例項

<?xml version="

1.0" encoding="

utf-16

"?>

"" xmlns:xsd="

">

hishis5

1234

測試1

二、將xml轉換成實體類

把xml轉換成相應的實體類,需要使用到xmlserializer類的deserialize方法,將xml進行反序列化。

public

static t deserializer(string strxml) where t:class

}catch

(exception ex)

}

示例:將上例中序列化後的xml反序列化成實體類

//反序列化

request r = xmlserializehelper.deserializer(strxml);

三、將datatable轉換成xml

複製**

//將datatable轉換成xml

datatable dt = new datatable("mytable");

//新增列

dt.columns.add("id", typeof(int));

dt.columns.add("name", typeof(string));

dt.columns.add("***", typeof(char));

//新增行

dt.rows.add(1, "小明", '1');

dt.rows.add(2, "小紅", '2');

dt.rows.add(3, "小王", '2');

dt.rows.add(4, "測試", '2');

//序列化,將datatable轉換成xml格式的字串

string strxml = xmlserializehelper.xmlserialize (dt);

複製**

四、將xml轉換成datatable

1 //反序列化,將xml轉換成字串

2 datatable dtnew=  xmlserializehelper.deserializer(strxml);

五、將list集合轉換成xml

複製**

///

/// 測試類

///

public class student

public string name

public char ***

public int age

}//測試集合

listlist = new list()

,new student(),

new student(),

new student()

};//序列化

string strxml = xmlserializehelper.xmlserialize>(list);

複製**

六、將xml轉換成集合

使用上面例子中集合轉換成的xml進行反序列化。

//反序列化

listliststu = xmlserializehelper.deserializer>(strxml);

posted @

2019-01-22 13:40

**38 閱讀(

...)

編輯收藏

map和實體類的相互轉換

方式1 成員變數為包裝類簡單屬性的用此方法 import org.springframework.cglib.beans.beanmap 實體類轉map beanmap.create entityobj map轉實體類 testbean bean maptobean map,new testbean...

XML和JSON相互轉換類 JAVA

package com.qs.util import net.sf.json.jsonobject import net.sf.json.xml.xmlserializer xml和json相互轉化類 需要的jar包 json lib 2.1.jar ezmorph 1.0.3.jar xom 1....

MyBatisPlus實現實體類注入列舉型別字段

為了加強實體類中,特殊字段型別的可讀性和省去資料型別的轉換,我們今天嘗試把int型別,例如狀態這種整型欄位換做列舉,下面是舊 data equalsandhashcode callsuper false accessors chain true tablename gdt targeting api...