C# XML对象序列化、反序列化 - PEPE YU
http://www.tuicool.com/articles/IjE7ban
http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795.html
XML 序列化:可以将对象序列化为XML文件,或者将XML文件反序列化为对象还有种方法 使用LINQ TO XML或者反序列化的方法从XML中读取数据。
最简单的方法就是 .net framework提供的,只需要定义好xml数据格式,定义好对象即可。
1、调用方法:
using RenosData.RDBao.EvidenceUploadModel.Models;
using RenosData.RDBao.EvidenceUploadModel.Parsers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\PEPE\Desktop\证据接口结构.xml";
FileStream stream = new FileStream(path, FileMode.Open);
EvidenceFilesModel dep = (EvidenceFilesModel)XMLParser.Deserialize(typeof(EvidenceFilesModel), stream);
stream.Close();
}
}
}
2、xml解析类
using RenosData.RDBao.EvidenceUploadModel.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace RenosData.RDBao.EvidenceUploadModel.Parsers
{
public class XMLParser
{
public T Deserialize<T>(T t, string s)
{
using (StringReader sr = new StringReader(s))
{
XmlSerializer xz = new XmlSerializer(t.GetType());
return (T)xz.Deserialize(sr);
}
}
public EvidenceFilesModel Convert(string xml)
{ //将xml反序列化成对象
EvidenceFilesModel b = new EvidenceFilesModel();
return Deserialize<EvidenceFilesModel>(b, xml);
}
#region 反序列化
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="xml">XML字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string xml)
{
try
{
using (StringReader sr = new StringReader(xml))
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(sr);
}
}
catch (Exception e)
{
return null;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type"></param>
/// <param name="xml"></param>
/// <returns></returns>
public static object Deserialize(Type type, Stream stream)
{
XmlSerializer xmldes = new XmlSerializer(type);
return xmldes.Deserialize(stream);
}
#endregion
#region 序列化XML文件
/// <summary>
/// 序列化XML文件
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer(Type type, object obj)
{
MemoryStream Stream = new MemoryStream();
//创建序列化对象
XmlSerializer xml = new XmlSerializer(type);
try
{
//序列化对象
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
return str;
}
#endregion
}
}
3、xml对象
根据xml定义的对象如下
eg 根目录类,其他的类就按xml格式一路下去
/// <summary>
/// 证据接口结构模型
/// </summary>
[Serializable]
[XmlRoot("EvidenceFiles")]
public class EvidenceFilesModel
{
/// <summary>
/// 证据包信息
/// </summary>
public EvidencePackageModel EvidencePackage { get; set; }
/// <summary>
/// 证据信息明细
/// </summary>
[XmlArrayItem("Evidence")]
public List<EvidenceModel> Evidences { get; set; }
}
注意特性:[XmlArrayItem("Evidence")] 这个下面是会有多个分支
4、XML代码
<?xml version="1.0" encoding="utf-8"?>
<EvidenceFiles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--证据包信息-->
<EvidencePackage>
<Version>1.0</Version>
<Package>
<TrackNO>TrackNO</TrackNO>
<Name>Name</Name>
<Description>
Description
</Description>
<ThumbFileName>
ThumbFileName
</ThumbFileName>
<Time>2011-10-24T15:35:57</Time>
</Package>
<Integrity>
<EvidenceCount>2</EvidenceCount>
<EvidenceSize>1024</EvidenceSize>
<Algorithm>MD5</Algorithm>
<AlgorithmValue>算法的值</AlgorithmValue>
</Integrity>
<Owner>
<Name>张三</Name>
<Identity>编号</Identity>
<Credentials>证件号</Credentials>
</Owner>
<GatherApp>
<AppId>标识</AppId>
<AppKey>验证码</AppKey>
<Version>版本号</Version>
</GatherApp>
</EvidencePackage>
<Evidences>
<Evidence>
<EvidenceMetaData>
<EvidenceUser>
<GatherUser>
<Name>张三</Name>
<Identity>编号</Identity>
<Credentials>证件号</Credentials>
</GatherUser>
</EvidenceUser>
<Environment>
<CreateTime>2014-1-1T15:00:00</CreateTime>
<Location>地点</Location>
<GenerationMethod>成方式-</GenerationMethod>
</Environment>
<GatherApp>
<AppName>系统名称</AppName>
<Version>版本号</Version>
</GatherApp>
<Parser>
<Encrypt>
<Algorithm>算法</Algorithm>
<AlgorithmVersion>版本号</AlgorithmVersion>
</Encrypt>
<ParserApp>
<FileExtension>.doc</FileExtension>
<FileType>类型</FileType>
<AppName>名称</AppName>
<AppVersion>版本</AppVersion>
</ParserApp>
</Parser>
<SourceFile>
<FileName>文件名</FileName>
<CreateTime>2014-1-1T15:00:00</CreateTime>
</SourceFile>
</EvidenceMetaData>
<Signature>
<UserSignature>
<Identity>账号</Identity>
<Algorithm>签名算法</Algorithm>
<SignKey>签名算法密钥</SignKey>
<SignValue>签名的值</SignValue>
</UserSignature>
</Signature>
</Evidence>
<Evidence>
<EvidenceMetaData>
<EvidenceUser>
<GatherUser>
<Name>张三</Name>
<Identity>编号</