实体类转换成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. 训练自己数据-xml文件转voc格式

    首先我们有一堆xml文件 笔者是将mask-rcnn得到的json标注文件转为xml的 批量json转xml方法:https://www.cnblogs.com/bob-jianfeng/p/1112 ...

  2. nuxtjs踩坑指南

    1.nuxt引入问题:Can't resolve 'stylus-loader' 原因在于没有安装stylus,安装即可:npm install stylus stylus-loader --save ...

  3. Spring框架spring-web模块中的RestTemplate类详解

    RestTemplate类是spring-web模块中进行HTTP访问的REST客户端核心类.RestTemplate请求使用阻塞式IO,适合低并发的应用场景. 1. RestTemplate类提供了 ...

  4. Unreal Engine 4 系列教程 Part 8:粒子系统教程

    .katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...

  5. matplotlib动态绘图

    目录 package Process 解决中文乱码问题 simple_plot() scatter_plot() three_dimension_scatter() Jupyter notebook ...

  6. HttpUtility.HtmlDecode ,HttpUtility.HtmlEncode 与 Server.HtmlDecode ,Server.HtmlEncode 与 HttpServerUtility.HtmlDecode , HttpServerUtility.HtmlEncode

    HtmlEncode: 将 Html 源文件中不允许出现的字符进行编码,通常是编码以下字符"<".">"."&" 等. ...

  7. ros局部路径规划-DWA学习

    ROS的路径规划器分为全局路径和局部路径规划,其中局部路径规划器使用的最广的为dwa,个人理解为: 首先全局路径规划会生成一条大致的全局路径,局部路径规划器会把全局路径给分段,然后根据分段的全局路径的 ...

  8. oracle导入Excel表文本数据

    首先导Excel表数据要先建和Excel表字段对应的表,然后将Excel表另存为Txt文本, 然后在Plsql客户端点击工具->文本导入器 然后这里要选择用户及其表,点击导入数据就可以

  9. Jvisualvm简单使用教程

    本博客介绍一下jvisualvm的简单使用教程,jvisualvm功能还是挺多的,不过本博客之简单介绍一下 1.拿线程快照信息 在jdk安装目录找到jvisualvm.exe,${JDK_HOME}\ ...

  10. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...