C#model类与XML转换类
来源:互联网 发布:美恰软件 编辑:程序博客网 时间:2024/05/16 16:04
Json作为现在API中通用的数据传输格式,目前有Newtonsoft.Json.dll进行转换,那么XML是不是有相关的方法可以转换呢?
原来也是有的,不过需要一定的编写。
转自https://www.cnblogs.com/dotnet261010/p/6513618.html
1、代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;using System.Xml.Serialization;namespace OAServiceInvoke{ public class XMLSerialize { public static string XmlSerialize<T>(T obj) { using (StringWriter sw = new StringWriter()) { Type t = obj.GetType(); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(sw, obj); sw.Close(); return sw.ToString(); } } 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 { return null; } } }}
2、序列化常用Attribute讲解说明:转自http://blog.csdn.net/zzy7075/article/details/50770062
[XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=false)] // 当该类为Xml根节点时,以此为根节点名称。
public class City
[XmlAttribute("AreaName")] // 表现为Xml节点属性。<... AreaName="..."/>
public string Name
[XmlElementAttribute("AreaId", IsNullable = false)] // 表现为Xml节点。<AreaId>...</AreaId>
public string Id
[XmlArrayAttribute("Areas")] // 表现为Xml层次结构,根为Areas,其所属的每个该集合节点元素名为类名。<Areas><Area ... /><Area ... /></Areas>
public Area[] Areas
[XmlElementAttribute("Area", IsNullable = false)] // 表现为水平结构的Xml节点。<Area ... /><Area ... />...
public Area[] Areas
[XmlIgnoreAttribute] // 忽略该元素的序列化。
把这些属性与model类的相关属性,配合使用,就可以自由设置相关XML的具体格式了。
比如如下的Model类
[XmlRootAttribute("MyCity", Namespace = "abc.abc", IsNullable = false)] public class City { [XmlAttribute("CityName")] public string Name { get; set; } [XmlAttribute("CityId")] public string Id { get; set; } [XmlArrayAttribute("Areas")] public Area[] Areas { get; set; } } [XmlRootAttribute("MyArea")] public class Area { [XmlAttribute("AreaName")] public string Name { get; set; } [XmlElementAttribute("AreaId", IsNullable = false)] public string Id { get; set; } [XmlElementAttribute("Street", IsNullable = false)] public string[] Streets { get; set; } }
经过如下的代码转换:
static void Main(string[] args) { Area area1 = new Area(); area1.Name = "Pudong"; area1.Id = "PD001"; area1.Streets = new string [] { "street 001", "street 002" }; Area area2 = new Area(); area2.Name = "Xuhui"; area2.Id = "XH002"; area2.Streets = new string [] { "street 003", "street 004" }; City city1 = new City(); city1.Name = "Shanghai"; city1.Id = "SH001"; city1.Areas = new Area[] { area1, area2 }; XmlSerializer.SaveToXml(@"C:\temp\XML\output003.xml", city1); }
就变成这样的XML
<?xml version="1.0" encoding="utf-8"?><MyCity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CityName="Shanghai" CityId="SH001" xmlns="abc.abc"> <Areas> <Area AreaName="Pudong"> <AreaId>PD001</AreaId> <Street>street 001</Street> <Street>street 002</Street> </Area> <Area AreaName="Xuhui"> <AreaId>XH002</AreaId> <Street>street 003</Street> <Street>street 004</Street> </Area> </Areas></MyCity>
3、如果要去掉根节点的xmlns:xsd和xmlns:xsi属性
则需要将序列化的方法修改为:
public static string XmlSerialize<T>(T obj) { using (StringWriter sw = new StringWriter()) { Type t = obj.GetType(); // 强制指定命名空间,覆盖默认的命名空间 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); XmlSerializer serializer = new XmlSerializer(obj.GetType()); // 序列化时增加namespaces serializer.Serialize(sw, obj, namespaces); sw.Close(); return sw.ToString(); } }
知行办公,专业移动办公平台
https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】运维艄公,897221533@qq.com;
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;
【安全】保密,你懂的。
- C#model类与XML转换类
- Model与XML互相转换
- 字典与Model类
- Model类与ImageDownLoader
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- xml与DataSet的互转换类
- XML与DataSet的相互转换类
- xml与DataSet的互转换类
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- xml与DataSet的互转换类
- XML与DataSet的相互转换类
- XML与DataSet的相互转换类
- 实体类与xml互相转换通用
- Algorithm之路八:String to Imteger
- Delaunay三角网生成算法
- [转载]Ubuntu 16.04编译安装OpenCV(Python2 /python3)(使用虚拟环境)
- 快速删除所有空值:汪琪玩Excel第十四招
- Go Context包使用
- C#model类与XML转换类
- 使用python实现正整数的阶乘
- LMA和VMA
- 前端小白--CSS实现水平垂直居中
- FGSM(Fast Gradient Sign Method)生成对抗样本(32)---《深度学习》
- Vegas文字投影的模糊效果怎么制作?
- Unity响应Android的返回键,Home键
- Android bat批处理自动执行adb shell命令
- NFS开发环境搭建