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 ...
随机推荐
- 再次体会wireshark的威力!
今天一位同学给我打电话,说是重启了客户机房里的一台服务器A,重启之后此台服务器不能与B服务器通信了,要命的是A台服务器上有关键数据需要与B服务器进行通信交互.客户大发雷霆,同学的老板也发火一再催促要尽 ...
- Pwnable-leg
Download : http://pwnable.kr/bin/leg.c Download :http://pwnable.kr/bin/leg.asm 友链 https://blog.csdn. ...
- 《Zabbix》
https://github.com/itnihao/zabbix-rpm https://github.com/itnihao/zabbix-book 一.zabbix支持的主要监控方式: zabb ...
- BootStrap、jQuery UI、bxSlider组件使用
组件的使用 首先需要将组件下载下来放在统同级目录下 导入组件 使用组件 BootStrap 示例: <!DOCTYPE html> <html lang="en" ...
- python--小确幸
#把手机号中间四位隐藏 def change_number(number): hiding_number=number.replace(number[3:7],'*'*4) print(hiding_ ...
- luogu4570 元素
题目链接 problem 有\(n\)个二元组, \((x,y)\),要选出一些二元组,使得他们的\(x\)的任何一个子集的异或和不为\(0\)并且\(y\)的和最大. solution 考虑是\(x ...
- UAC简介
用户帐户控制 (User Account Control) 是Windows Vista(及更高版本操作系统)中一组新的基础结构技术,可以帮助阻止恶意程序(有时也称为“恶意软件”)损坏系统,同时也可以 ...
- 第02组 Beta冲刺(5/5)
队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 验收了小程序的主要功能 制作Beta展示所需要用到的视频 制作Beta展示PPT 准备Beta答辩 提交记录(全组共用) ...
- npm ERR! Cannot read property 'resolve' of undefined
一 .有可能是版本过低,或者软件损坏,重新安装一下试试 地址
- Prometheus神器之监控K8s集群
Prometheus 简介 Prometheus是SoundCloud开源的一款开源软件.它的实现参考了Google内部的监控实现,与源自Google的Kubernetes结合起来非常合适.另外相比i ...