码迷,mamicode.com
首页 > Windows程序 > 详细

C#实体类(复杂类)与XML互相转换

时间:2019-12-13 09:32:57      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:meta   join   utf-16   db2   writer   logs   phone   XML   als   

实体类转换成XML方法:

将实体类转换成XML需要使用XmlSerializer类的Serialize方法,将实体类序列化

  1.  
    public static string XmlSerialize<T>(T obj)
  2.  
    {
  3.  
    using (System.IO.StringWriter sw = new StringWriter())
  4.  
    {
  5.  
    Type t = obj.GetType();
  6.  
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
  7.  
    serializer.Serialize(sw, obj);
  8.  
    sw.Close();
  9.  
    return sw.ToString();
  10.  
    }
  11.  
    }

例子:

定义实体类

  1.  
    public class root
  2.  
    {
  3.  
    public head head { get; set; }
  4.  
    public body body { get; set; }
  5.  
    }
  6.  
     
  7.  
    public class head
  8.  
    {
  9.  
    public string organ { get; set; }
  10.  
    public string jkxlh { get; set; }
  11.  
    public string jkid { get; set; }
  12.  
    }
  13.  
     
  14.  
    //注意!body类的vehispara的类型是dynamic 所以需要使用XmlInclude表示body可以解析的类型
  15.  
    [System.Xml.Serialization.XmlInclude(typeof(JCZ01))]
  16.  
    [System.Xml.Serialization.XmlInclude(typeof(JCZ02))]
  17.  
    public partial class body
  18.  
    {
  19.  
    public dynamic vehispara { get; set; }//接受动态业务类型 即JCZ01、JCZ02等等
  20.  
    }
  21.  
     
  22.  
    public class JCZ01
  23.  
    {
  24.  
    public string tsno { get; set; }
  25.  
    public string orgcode { get; set; }
  26.  
    public string teststation { get; set; }
  27.  
    public string testaddress { get; set; }
  28.  
    public DateTime? firstauthdate { get; set; }
  29.  
    public DateTime? jlrzyxrq { get; set; }
  30.  
    public DateTime? linkdate { get; set; }
  31.  
    public string legalperson { get; set; }
  32.  
    public string test { get; set; }
  33.  
    public string testtel { get; set; }
  34.  
    public int testlines { get; set; }
  35.  
    public string status { get; set; }
  36.  
    public decimal? lng { get; set; }
  37.  
    public decimal? lat { get; set; }
  38.  
    }
  39.  
     
  40.  
    public class JCZ02
  41.  
    {
  42.  
    public string tsno { get; set; }
  43.  
    public string testlineno { get; set; }
  44.  
    public string firstauthdate { get; set; }
  45.  
    public string testtype { get; set; }
  46.  
    public string status { get; set; }
  47.  
    public string gwip { get; set; }
  48.  
    public string lxpzq { get; set; }
  49.  
    public string lxpzh { get; set; }
  50.  
    public string lxpzczj { get; set; }
  51.  
    public string lxpzdt { get; set; }
  52.  
    public string jclc { get; set; }
  53.  
    public string jcbbh { get; set; }
  54.  
    public string provider { get; set; }
  55.  
    public string testexpiredade { get; set; }
  56.  
    public string dynamometer { get; set; }
  57.  
    public string dprovider { get; set; }
  58.  
    public string dadate { get; set; }
  59.  
    public string analyser { get; set; }
  60.  
    public string aprovider { get; set; }
  61.  
    public string aadate { get; set; }
  62.  
    public string flowmeter { get; set; }
  63.  
    public string fprovider { get; set; }
  64.  
    public string fadate { get; set; }
  65.  
    public string smokemeter { get; set; }
  66.  
    public string sprovider { get; set; }
  67.  
    public string sadate { get; set; }
  68.  
    public string tachometer { get; set; }
  69.  
    public string tprovider { get; set; }
  70.  
    public string tadate { get; set; }
  71.  
    public string otsensor { get; set; }
  72.  
    public string oprovider { get; set; }
  73.  
    public string oadate { get; set; }
  74.  
    public string wstype { get; set; }
  75.  
    public string wsrovider { get; set; }
  76.  
    }

实体类转XML代码

  1.  
    //Linq to sql 获取数据
  2.  
    var query = from sta in det.Org_DetectStation join line in lineCount on sta.StaID equals line.StaID
  3.  
    where sta.StaID == staid
  4.  
    select new JCZ01
  5.  
    {
  6.  
    tsno = cityid + sta.StaID.Substring(4,2),
  7.  
    orgcode = cityid + sta.StaID.Substring(4, 2),
  8.  
    teststation = sta.StaName,
  9.  
    testaddress = sta.Address,
  10.  
    firstauthdate = sta.CMADate,
  11.  
    jlrzyxrq = sta.CMADate,
  12.  
    linkdate = sta.CMADate,
  13.  
    legalperson = sta.CEOName,
  14.  
    test = sta.CEOName,
  15.  
    testtel = sta.CEOOfficePhone,
  16.  
    testlines = line.LineCount,
  17.  
    status = sta.StaState==0?"1":"2",
  18.  
    lng = sta.Longitude,
  19.  
    lat = sta.Latitude
  20.  
    };
  21.  
    List<JCZ01> jcz011 = query.ToList<JCZ01>();
  22.  
    root r = new root();
  23.  
    head h = new head();
  24.  
    h.jkid = Properties.Settings.Default.JKBH;
  25.  
    h.jkxlh = Properties.Settings.Default.JKXLH;
  26.  
    body b = new body();
  27.  
    b.vehispara = jcz011[0];
  28.  
    r.head = h;
  29.  
    r.body = b;
  30.  
     
  31.  
    string strhxml = XmlSerialize<head>(h);
  32.  
    string strbxml = XmlSerialize<body>(b);
  33.  
    string strrxml = XmlSerialize<root>(r);

生成的XML实例

  1.  
    <?xml version="1.0" encoding="utf-16"?>
  2.  
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  3.  
    <head>
  4.  
    <jkxlh>BD11F82096F0290DB2866BD266A0CEDF</jkxlh>
  5.  
    <jkid>23270000</jkid>
  6.  
    </head>
  7.  
    <body>
  8.  
    <vehispara xsi:type="JCZ01">
  9.  
    <tsno>23270002</tsno>
  10.  
    <orgcode>23270002</orgcode>
  11.  
    <teststation>XXXX有限公司</teststation>
  12.  
    <testaddress>测试</testaddress>
  13.  
    <firstauthdate>2038-08-11T00:00:00</firstauthdate>
  14.  
    <jlrzyxrq>2038-08-11T00:00:00</jlrzyxrq>
  15.  
    <linkdate>2038-08-11T00:00:00</linkdate>
  16.  
    <legalperson>测试</legalperson>
  17.  
    <test>测试</test>
  18.  
    <testtel />
  19.  
    <testlines>1</testlines>
  20.  
    <status>1</status>
  21.  
    <lng xsi:nil="true" />
  22.  
    <lat xsi:nil="true" />
  23.  
    </vehispara>
  24.  
    </body>
  25.  
    </root>

 

XML转实体类:

把XML转换成相应的实体类,需要使用到XmlSerializer类的Deserialize方法,将XML进行反序列化

  1.  
    public static T DESerializer<T>(string strXML) where T:class
  2.  
    {
  3.  
    try
  4.  
    {
  5.  
    using (StringReader sr = new StringReader(strXML))
  6.  
    {
  7.  
    XmlSerializer serializer = new XmlSerializer(typeof(T));
  8.  
    return serializer.Deserialize(sr) as T;
  9.  
    }
  10.  
    }
  11.  
    catch (Exception ex)
  12.  
    {
  13.  
    return null;
  14.  
    }
  15.  
    }

实体类转XML需注意的问题:

当实体类的string类型字段为null时,序列化成XML时会忽略掉这个字段(即序列化后的XML中没有该节点)

解决的办法我知道的有两个,第一个把该字段赋值为空字符串,另一个就是给类的属性加上XmlElement(IsNullable=true)特性,如:

  1.  
    public class JCZ01
  2.  
    {
  3.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  4.  
    public string tsno { get; set; }
  5.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  6.  
    public string orgcode { get; set; }
  7.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  8.  
    public string teststation { get; set; }
  9.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  10.  
    public string testaddress { get; set; }
  11.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  12.  
    public DateTime? firstauthdate { get; set; }
  13.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  14.  
    public DateTime? jlrzyxrq { get; set; }
  15.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  16.  
    public DateTime? linkdate { get; set; }
  17.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  18.  
    public string legalperson { get; set; }
  19.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  20.  
    public string test { get; set; }
  21.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  22.  
    public string testtel { get; set; }
  23.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  24.  
    public int? testlines { get; set; }
  25.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  26.  
    public string status { get; set; }
  27.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  28.  
    public decimal? lng { get; set; }
  29.  
    [System.Xml.Serialization.XmlElement(IsNullable = true)]
  30.  
    public decimal? lat { get; set; }
  31.  
    }

 

参考:https://www.cnblogs.com/dotnet261010/p/6513618.html

 

出处:https://blog.csdn.net/CGS_______/article/details/84559590

C#实体类(复杂类)与XML互相转换

标签:meta   join   utf-16   db2   writer   logs   phone   XML   als   

原文地址:https://www.cnblogs.com/mq0036/p/12033131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!