实体类转换成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. 面向对象程序设计(JAVA) 第10周学习指导及要求

    2019面向对象程序设计(Java)第10周学习指导及要求 (2019.11.1-2019.11.4)   学习目标 1.掌握java异常处理技术: 2.了解断言的用法: 3.了解日志的用途: 4.掌 ...

  2. Python网络编程基础 ❶ 计算机网络基础 初用socket模块

    1.计算机网络基础 C/S  客户端/服务器端 B/S  浏览器端/服务器端 mac地址,ip地址,子网掩码,与计算得到网段,端口号每台计算机的网卡都有全球唯一的地址,在生产时已经写进去了. ip地址 ...

  3. 【oracle】查看表空间信息

    -- 数据库查询表空间使用情况SELECT Upper(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB/1024 "表空间大小(G ...

  4. Python高级应用程序设计任务期末作业

    Python高级应用程序设计任务要求 用Python实现一个面向主题的网络爬虫程序,并完成以下内容:(注:每人一题,主题内容自选,所有设计内容与源代码需提交到博客园平台) 一.主题式网络爬虫设计方案( ...

  5. 小白专场-FileTransfer-python语言实现

    目录 更新.更全的<数据结构与算法>的更新网站,更有python.go.人工智能教学等着你:https://www.cnblogs.com/nickchen121/p/11407287.h ...

  6. Mac流程图的软件

    里面有破解机器,按照步骤一步步来就可以了 https://www.zhinin.com/omnigraffle_pro-mac.html

  7. prisma反向代理

    概要 为什么要做 prisma 的反向代理 反向代理示例(by golang) prisma 服务 gateway 服务 整体流程 认证 反向代理 权限 总结 概要 接触 prisma 有段时间了, ...

  8. mybatis批处理数据

    批处理数据主要有三种方式: 1.传统jdbc处理    2.mybatis批处理插入    3.使用executortype处理 jdbc 处理 1.通过 for循环插入 main方法如下所示: Co ...

  9. java、JavaScript获取微信用户信息登录优化方案

    1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...

  10. AutoDesk公司搞的fbx模型格式

    FBX® data exchange technology is a 3D asset exchange format that facilitates higher-fidelity data ex ...