实体类转换成XML方法:
将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化
-
public static string XmlSerialize<T>(T obj)
-
-
using (System.IO.StringWriter sw = new StringWriter())
-
-
-
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
-
serializer.Serialize(sw, obj);
-
-
-
-
例子:
定义实体类
-
-
-
public head head { get; set; }
-
public body body { get; set; }
-
-
-
-
-
public string organ { get; set; }
-
public string jkxlh { get; set; }
-
public string jkid { get; set; }
-
-
-
-
[System.Xml.Serialization.XmlInclude(typeof(JCZ01))]
-
[System.Xml.Serialization.XmlInclude(typeof(JCZ02))]
-
public partial class body
-
-
public dynamic vehispara { get; set; }
-
-
-
-
-
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 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代码
-
-
var query = from sta in det.Org_DetectStation join line in lineCount on sta.StaID equals line.StaID
-
-
-
-
tsno = cityid + sta.StaID.Substring(4,2),
-
orgcode = cityid + sta.StaID.Substring(4, 2),
-
teststation = sta.StaName,
-
testaddress = sta.Address,
-
firstauthdate = sta.CMADate,
-
-
-
legalperson = sta.CEOName,
-
-
testtel = sta.CEOOfficePhone,
-
testlines = line.LineCount,
-
status = sta.StaState==0?"1":"2",
-
-
-
-
List<JCZ01> jcz011 = query.ToList<JCZ01>();
-
-
-
h.jkid = Properties.Settings.Default.JKBH;
-
h.jkxlh = Properties.Settings.Default.JKXLH;
-
-
-
-
-
-
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">
-
-
<jkxlh>BD11F82096F0290DB2866BD266A0CEDF</jkxlh>
-
-
-
-
<vehispara xsi:type="JCZ01">
-
-
<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>
-
-
-
-
-
-
-
-
-
XML转实体类:
把XML转换成相应的实体类,需要使用到XmlSerializer类的Deserialize方法,将XML进行反序列化
-
public static T DESerializer<T>(string strXML) where T:class
-
-
-
-
using (StringReader sr = new StringReader(strXML))
-
-
XmlSerializer serializer = new XmlSerializer(typeof(T));
-
return serializer.Deserialize(sr) as T;
-
-
-
-
-
-
-
实体类转XML需注意的问题:
当实体类的string类型字段为null时,序列化成XML时会忽略掉这个字段(即序列化后的XML中没有该节点)
解决的办法我知道的有两个,第一个把该字段赋值为空字符串,另一个就是给类的属性加上XmlElement(IsNullable=true)特性,如:
-
-
-
[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; }
-