实体类转换成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互相转换的更多相关文章

  1. 利用JAXB实现java实体类和xml互相转换

    1.应用场景 在使用WebService实现数据上传下载,数据查询时,可以利用JAXB实现java实体类和xml互相转换 2.Demo 2.1 student.java 实体类,包含list(set同 ...

  2. xsd与xml和类(class)对象之间的互相转换

    xsd与xml和类(class)对象之间的互相转换 . 第一:通过现有的已经写好的xsd来生成class(.cs)文件. 在您Visual Studio的安装目录下的SDKv2.0Bin中有个应用程序 ...

  3. c# 实体处理工具类

    using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...

  4. org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 前言中不允许有内容 来自类路径资源的XML文档中的第1行是无效的

    今天复习一下Spring和Hibernate的整合,遇到了一个问题,报错信息如下: org.springframework.beans.factory.xml.XmlBeanDefinitionSto ...

  5. UML类图及类与类之间的关系

    原文地址:http://www.uml.org.cn/oobject/201211231.asp 类图用于描述系统中所包含的类以及它们之间的相互关系,帮助人们简化对系统的理解,它是系统分析和设计阶段的 ...

  6. 图解UML类与类之间的六中关系

    大话设计模式上的一个图,我用EA画出来的:  UML中的6大关系相关英文及音标:  依赖关系 dependency [di'pendənsi]  关联关系 association  [ə,səuʃi' ...

  7. 2.java面向对象类与类/类与对象之间关系详解

    继承.实现.依赖.关联.聚合.组合的联系与区别 下面的内容很基础,同时也很简单,但是也很重要. 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功 ...

  8. 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类

    在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...

  9. java 类与类,类与接口 ,接口与接口关系

    类: 生活中类是人们对客观事物不断认识而产生的抽象概念,而对象则是现实生活中的一个个实体 面向对象程序设计中,对象是程序的基本单位,相似的对象像变量和类型的关系一样归并到一类,所以,并不先具体地定义对 ...

  10. JAVA类与类之间的全部关系简述+代码详解

    本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...

随机推荐

  1. 访问https问题

    访问https问题 package com.yuantiao.smartcardms.tools; import com.alibaba.fastjson.JSONObject; import com ...

  2. 剑指Offer-18.二叉树的镜像(C++/Java)

    题目: 题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ ...

  3. Visual Studio 2017 编译 gens 世嘉模拟器

    1) 下载源代码 2.14 版本 http://gens.me/downloads.shtml 2) 下载 老版本 DirectX 并且安装 https://github.com/apitrace/a ...

  4. JQuerys实现三级省市联动

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. 阻止iOS Web APP中点击链接跳转到Safari 浏览器新标签页

    问题:ios封装完之后,点击里边的按钮会跳转到网页上 ——小卡遇到这个问题就是这样解决的↓↓↓ 解决方法:建议将代码放到</head>标签前,当然,另外存为一个js 文件引用也是可以的呦~ ...

  6. 使用Vue Baidu Map对百度地图实现输入框搜索定位

    前端时间需要在页面的输入框输入地址,搜索并在百度地图上获取选定结果的坐标,前端使用了Vue + Element-ui,地图方面直接使用了封装好的百度地图vue组件-vue-baidu-map     ...

  7. 基于仿射的非刚体配准方法(i) 法向

    为啥闲呢,因为work干完了. 为啥补档呢,因为有新work了. 呃,因为新work让人自闭. 我现在干完了两部分.一是把最近邻的部分迁移过来. 二是求法向. 首先是给三个点,就能确定平面——因为是三 ...

  8. ssh框架被淘汰的原因

    SSH就是Struts2+Spring+Hibernate. 三个组件的简单介绍 Struts2:通俗的讲就是为了完成MVC模型中的C的功能,也就是编写具体的业务逻辑的地方.从他的设计上来看就是请求到 ...

  9. C# 爬虫相关的、可供参考的开源项目

    1. Abots https://github.com/sjdirect/abot/ 2. DotnetSpider https://github.com/dotnetcore/DotnetSpide ...

  10. 云原生生态周报 Vol. 13 | Forrester 发布企业级容器平台报告

    业界要闻 近日,全球知名市场调研机构 Forrester 发布首个企业级公共云容器平台报告.其中,阿里云容器服务的市场表现全球前三.中国第一,同时创造中国企业最好成绩,进入强劲表现者象限.报告显示,阿 ...