C#实体类(复杂类)与XML互相转换
实体类转换成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(,),
orgcode = cityid + sta.StaID.Substring(, ),
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==?"":"",
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[];
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
C#实体类(复杂类)与XML互相转换的更多相关文章
- 利用JAXB实现java实体类和xml互相转换
1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...
- xsd与xml和类(class)对象之间的互相转换
xsd与xml和类(class)对象之间的互相转换 . 第一:通过现有的已经写好的xsd来生成class(.cs)文件. 在您Visual Studio的安装目录下的SDKv2.0Bin中有个应用程序 ...
- c# 实体处理工具类
using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...
- org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 前言中不允许有内容 来自类路径资源的XML文档中的第1行是无效的
今天复习一下Spring和Hibernate的整合,遇到了一个问题,报错信息如下: org.springframework.beans.factory.xml.XmlBeanDefinitionSto ...
- UML类图及类与类之间的关系
原文地址:http://www.uml.org.cn/oobject/201211231.asp 类图用于描述系统中所包含的类以及它们之间的相互关系,帮助人们简化对系统的理解,它是系统分析和设计阶段的 ...
- 图解UML类与类之间的六中关系
大话设计模式上的一个图,我用EA画出来的: UML中的6大关系相关英文及音标: 依赖关系 dependency [di'pendənsi] 关联关系 association [ə,səuʃi' ...
- 2.java面向对象类与类/类与对象之间关系详解
继承.实现.依赖.关联.聚合.组合的联系与区别 下面的内容很基础,同时也很简单,但是也很重要. 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功 ...
- 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类
在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...
- java 类与类,类与接口 ,接口与接口关系
类: 生活中类是人们对客观事物不断认识而产生的抽象概念,而对象则是现实生活中的一个个实体 面向对象程序设计中,对象是程序的基本单位,相似的对象像变量和类型的关系一样归并到一类,所以,并不先具体地定义对 ...
- JAVA类与类之间的全部关系简述+代码详解
本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...
随机推荐
- Vue入门案例(二)
创建一个 .html 文件,然后通过如下方式引入 Vue: <!-- 开发环境版本,包含了有帮助的命令行警告 --> <script src="https://cdn.js ...
- 计算机体系结构:量化研究方法(中文第五版)_扫描版_23.5M.pdf
- Winform重写键盘按键事件
/// <summary> /// 重写键盘处理事件,处理退出和回车按钮 /// </summary> protected override bool ProcessCmdKe ...
- javaagent的实现
实现javaagent功能的是一个叫做instrument的JVMTIAgent(linux下对应的动态库是libinstrument.so),另外instrument agent还有个别名叫JPLI ...
- 快速傅立叶变换(FFT)
多项式 系数表示法 设\(f(x)\)为一个\(n-1\)次多项式,则 \(f(x)=\sum\limits_{i=0}^{n-1}a_i*x_i\) 其中\(a_i\)为\(f(x)\)的系数,用这 ...
- MySQL实战45讲学习笔记:第四十一讲
一.本节概述 我在上一篇文章最后,给你留下的问题是怎么在两张表中拷贝数据.如果可以控制对源表的扫描行数和加锁范围很小的话,我们简单地使用 insert … select 语句即可实现. 当然,为了避免 ...
- 原生js实现append()方法
一.在使用jquery时,append() 方法在被选元素的结尾(仍然在内部)插入指定内容 使用方法:$(selector).append(content),content为必需的.规定要插入的内容( ...
- 【Linux命令】Linux命令后面所接选项和参数的区别
Linux命令后面所接选项和参数的区别 在使用Linux命令时,有时候后面会跟一些"选项"(options)或"参数"(agruments) 命令格式为: #中 ...
- 我在生产项目里是如何使用Redis发布订阅的?(一)使用场景
转载请注明出处! 导语 Redis是我们很常用的一款nosql数据库产品,我们通常会用Redis来配合关系型数据库一起使用,弥补关系型数据库的不足. 其中,Redis的发布订阅功能也是它的一大亮点.虽 ...
- mycat 白话配置
1.server.xml <user name="root" defaultAccount="true"> <property name=&q ...