實體類轉換成XML方法:
將實體類轉換成XML需要使用XmlSerializer類的Serialize方法,將實體類序列化
public static string XmlSerialize<T>(T obj) { using (System.IO.StringWriter sw = new StringWriter()) { Type t = obj.GetType(); System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); serializer.Serialize(sw, obj); sw.Close(); return sw.ToString(); } }
例子:
定義實體類
public class root { public head head { get; set; } public body body { get; set; } } public class head { public string organ { get; set; } public string jkxlh { get; set; } public string jkid { get; set; } } //注意!body類的vehispara的類型是dynamic 所以需要使用XmlInclude表示body可以解析的類型 [System.Xml.Serialization.XmlInclude(typeof(JCZ01))] [System.Xml.Serialization.XmlInclude(typeof(JCZ02))] public partial class body { public dynamic vehispara { get; set; }//接受動態業務類型 即JCZ01、JCZ02等等 } public class JCZ01 { public string tsno { get; set; } public string orgcode { get; set; } public string teststation { get; set; } public string testaddress { get; set; } public DateTime? firstauthdate { get; set; } public DateTime? jlrzyxrq { get; set; } public DateTime? linkdate { get; set; } public string legalperson { get; set; } public string test { get; set; } public string testtel { get; set; } public int testlines { get; set; } public string status { get; set; } public decimal? lng { get; set; } public decimal? lat { get; set; } } public class JCZ02 { public string tsno { get; set; } public string testlineno { get; set; } public string firstauthdate { get; set; } public string testtype { get; set; } public string status { get; set; } public string gwip { get; set; } public string lxpzq { get; set; } public string lxpzh { get; set; } public string lxpzczj { get; set; } public string lxpzdt { get; set; } public string jclc { get; set; } public string jcbbh { get; set; } public string provider { get; set; } public string testexpiredade { get; set; } public string dynamometer { get; set; } public string dprovider { get; set; } public string dadate { get; set; } public string analyser { get; set; } public string aprovider { get; set; } public string aadate { get; set; } public string flowmeter { get; set; } public string fprovider { get; set; } public string fadate { get; set; } public string smokemeter { get; set; } public string sprovider { get; set; } public string sadate { get; set; } public string tachometer { get; set; } public string tprovider { get; set; } public string tadate { get; set; } public string otsensor { get; set; } public string oprovider { get; set; } public string oadate { get; set; } public string wstype { get; set; } public string wsrovider { get; set; } }
實體類轉XML代碼
//Linq to sql 獲取數據 var query = from sta in det.Org_DetectStation join line in lineCount on sta.StaID equals line.StaID where sta.StaID == staid select new JCZ01 { tsno = cityid + sta.StaID.Substring(4,2), orgcode = cityid + sta.StaID.Substring(4, 2), teststation = sta.StaName, testaddress = sta.Address, firstauthdate = sta.CMADate, jlrzyxrq = sta.CMADate, linkdate = sta.CMADate, legalperson = sta.CEOName, test = sta.CEOName, testtel = sta.CEOOfficePhone, testlines = line.LineCount, status = sta.StaState==0?"1":"2", lng = sta.Longitude, lat = sta.Latitude }; List<JCZ01> jcz011 = query.ToList<JCZ01>(); root r = new root(); head h = new head(); h.jkid = Properties.Settings.Default.JKBH; h.jkxlh = Properties.Settings.Default.JKXLH; body b = new body(); b.vehispara = jcz011[0]; r.head = h; r.body = b; string strhxml = XmlSerialize<head>(h); string strbxml = XmlSerialize<body>(b); string strrxml = XmlSerialize<root>(r);
生成的XML實例
<?xml version="1.0" encoding="utf-16"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <head> <jkxlh>BD11F82096F0290DB2866BD266A0CEDF</jkxlh> <jkid>23270000</jkid> </head> <body> <vehispara xsi:type="JCZ01"> <tsno>23270002</tsno> <orgcode>23270002</orgcode> <teststation>XXXX有限公司</teststation> <testaddress>測試</testaddress> <firstauthdate>2038-08-11T00:00:00</firstauthdate> <jlrzyxrq>2038-08-11T00:00:00</jlrzyxrq> <linkdate>2038-08-11T00:00:00</linkdate> <legalperson>測試</legalperson> <test>測試</test> <testtel /> <testlines>1</testlines> <status>1</status> <lng xsi:nil="true" /> <lat xsi:nil="true" /> </vehispara> </body> </root>
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需注意的問題:
當實體類的string類型字段為null時,序列化成XML時會忽略掉這個字段(即序列化后的XML中沒有該節點)
解決的辦法我知道的有兩個,第一個把該字段賦值為空字符串,另一個就是給類的屬性加上XmlElement(IsNullable=true)特性,如:
public class JCZ01 { [System.Xml.Serialization.XmlElement(IsNullable = true)] public string tsno { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string orgcode { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string teststation { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string testaddress { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public DateTime? firstauthdate { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public DateTime? jlrzyxrq { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public DateTime? linkdate { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string legalperson { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string test { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string testtel { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public int? testlines { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public string status { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public decimal? lng { get; set; } [System.Xml.Serialization.XmlElement(IsNullable = true)] public decimal? lat { get; set; } }
參考:https://www.cnblogs.com/dotnet261010/p/6513618.html
出處:https://blog.csdn.net/CGS_______/article/details/84559590