摘要:本文主要向大家介绍了C#编程之.net中序列化读写xml方法,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之.net中序列化读写xml方法,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
阅读目录
开始
最简单的使用XML的方法
类型定义与XML结构的映射
使用 XmlElement
使用 XmlAttribute
使用 InnerText
重命名节点名称
列表和数组的序列化
列表和数组的做为数据成员的序列化
类型继承与反序列化
反序列化的实战演练
反序列化的使用总结
排除不需要序列化的成员
强制指定成员的序列化顺序
自定义序列化行为
序列化去掉XML命名空间及声明头
XML的使用建议
XML是一种很常见的数据保存方式,我经常用它来保存一些数据,或者是一些配置参数。使用C#,我们可以借助.net framework提供的很多API来读取或者创建修改这些XML,然而,不同人使用XML的方法很有可能并不相同。今天我打算谈谈我使用XML的一些方法,供大家参考。
回到顶部
由于.net framework针对XML提供了很多API,这些API根据不同的使用场景实现了不同层次的封装,比如,我们可以直接使用XmlTextReader、XmlDocument、XPath来取数XML中的数据,也可以使用LINQ TO XML或者反序列化的方法从XML中读取数据。那么,使用哪种方法最简单呢?
我个人倾向于使用序列化,反序列化的方法来使用XML。采用这种方法,我只要考虑如何定义数据类型就可以了,读写XML各只需要一行调用即可完成。例如:
// 1. 首先要创建或者得到一个数据对象Order order = GetOrderById(123);// 2. 用序列化的方法生成XMLstring xml = XmlHelper.XmlSerialize(order, Encoding.UTF8);// 3. 从XML读取数据并生成对象Order order2 = XmlHelper.XmlDeserialize<Order>(xml, Encoding.UTF8);
就是这么简单的事情,XML结构是什么样的,我根本不用关心,我只关心数据是否能保存以及下次是否能将它们读取出来。
说明:XmlHelper是一个工具类,全部源代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;using System.IO;using System.Xml;// 此处代码来源于博客【在.net中读写config文件的各种方法】的示例代码// //www.cnblogs.com/fish-li/archive/2011/12/18/2292037.htmlnamespace MyMVC{ public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding) { if( o == null ) throw new ArgumentNullException("o"); if( encoding == null ) throw new ArgumentNullException("encoding"); XmlSerializer serializer = new XmlSerializer(o.GetType()); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineChars = "\r\n"; settings.Encoding = encoding; settings.IndentChars = " "; using( XmlWriter writer = XmlWriter.Create(stream, settings) ) { serializer.Serialize(writer, o); writer.Close(); } } /// <summary> /// 将一个对象序列化为XML字符串 /// </summary> /// <param name="o">要序列化的对象</param> /// <param name="encoding">编码方式</param> /// <returns>序列化产生的XML字符串</returns> public static string XmlSerialize(object o, Encoding encoding) { using( MemoryStream stream = new MemoryStream() ) { XmlSerializeInternal(stream, o, encoding); stream.Position = 0; using( StreamReader reader = new StreamReader(stream, encoding) ) { return reader.ReadToEnd(); } } } /// <summary> /// 将一个对象按XML序列化的方式写入到一个文件 /// </summary> /// <param name="o">要序列化的对象</param> /// <param name="path">保存文件路径</param> /// <param name="encoding">编码方式</param> public static void XmlSerializeToFile(object o, string path, Encoding encoding) { if( string.IsNullOrEmpty(path) ) throw new ArgumentNullException("path"); using( FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write) ) { XmlSerializeInternal(file, o, encoding); } } /// <summary> /// 从XML字符串中反序列化对象 /// </summary> /// <typeparam name="T">结果对象类型</typeparam> /// <param name="s">包含对象的XML字符串</param> /// <param name="encoding">编码方式</param> /// <returns>反序列化得到的对象</returns> public static T XmlDeserialize<T>(string s, Encoding encoding) { if( string.IsNullOrEmpty(s) ) throw new ArgumentNullException("s"); if( encoding == null ) throw new ArgumentNullException("encoding"); XmlSerializer mySerializer = new XmlSerializer(typeof(T)); using( MemoryStream ms = new MemoryStream(encoding.GetBytes(s)) ) { using( StreamReader sr = new StreamReader(ms, encoding) ) { return (T)mySerializer.Deserialize(sr); } } } /// <summary> /// 读入一个文件,并按XML的方式反序列化对象。 /// </summary> /// <typeparam name="T">结果对象类型</typeparam> /// <param name="path">文件路径</param> /// <param name="encoding">编码方式</param> /// <returns>反序列化得到的对象</returns> public static T XmlDeserializeFromFile<T>(string path, Encoding encoding) { if( string.IsNullOrEmpty(path) ) throw new ArgumentNullException("path"); if( encoding == null ) throw new ArgumentNullException("encoding"); string xml = File.ReadAllText(path, encoding); return XmlDeserialize<T>(xml, encoding); } }}
或许有人会说:我使用XPath从XML读取数据也很简单啊。
我认为这种说法有一个限制条件:只需要从XML中读取少量的数据。
如果要全部读取,用这种方法会写出一大堆的机械代码出来!所以,我非常反感用这种方法从XML中读取全部数据。
回到顶部
如果是一个新项目,我肯定会毫不犹豫的使用序列化和反序列化的方法来使用XML,然而,有时在维护一个老项目时,面对一堆只有XML却没有与之对应的C#类型时,我们就需要根据XML结构来逆向推导C#类型,然后才能使用序列化和反序列化的方法。逆向推导的过程是麻烦的,不过,类型推导出来之后,后面的事情就简单多了。
为了学会根据XML结构逆向推导类型,我们需要关注一下类型定义与XML结构的映射关系。
注意:有时候我们也会考虑XML结构对于传输量及可阅读性的影响,所以关注一下XML也是有必要的。
这里有一个XML文件,是我从Visual Sutdio的安装目录中找到的:
<DynamicHelp xmlns="//msdn.microsoft.com/vsdata/xsd/vsdh.xsd" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//msdn.microsoft.com/vsdata/xsd/vsdh.xsd vsdh.xsd"> <LinkGroup ID="sites" Title="Venus Sites" Priority="1500"> <Glyph Collapsed="3" Expanded="4"/> </LinkGroup> <LinkGroup ID="Venus Private Forums" Title="Venus Private Forums" Priority="1400"> <Glyph Collapsed="3" Expanded="4"/> </LinkGroup> <LinkGroup ID="ASP.NET Forums" Title="ASP.NET 1.0 Public Forums" Priority="1200"> <Glyph Collapsed="3" Expanded="4"/> </LinkGroup> <Context> <Links> <LItem URL="//www.asp.net/venus" LinkGroup="sites">Venus Home Page</LItem> <LItem URL="//www.asp.net" LinkGroup="sites">ASP.NET Home Page</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=77" LinkGroup="Venus Private Forums">General Discussions</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=83" LinkGroup="Venus Private Forums">Feature Requests</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=78" LinkGroup="Venus Private Forums">Bug Reports</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=86" LinkGroup="Venus Private Forums">ASP.NET 2.0 Related issues</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=11" LinkGroup="ASP.NET Forums">Announcements</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=15" LinkGroup="ASP.NET Forums">Getting Started</LItem> <LItem URL="//www.asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=18" LinkGroup="ASP.NET Forums">Web Forms</LItem> </Links> </Context></DynamicHelp>
怎样用反序列化的方式来读取它的数据呢,我在博客的最后将给出完整的实现代码。
现在,我们还是看一下这个XML有哪些特点吧。
<LinkGroup ID="sites" Title="Venus Sites" Priority="1500">
对于这个节点来说,它包含了三个数据项(属性):ID,Title,Priority。这样的LinkGroup节点有三个。
类似的还有Glyph节点。
<LItem URL="//www.asp.net" LinkGroup="sites">ASP.NET Home Page</LItem>
LItem节点除了与LinkGroup有着类似的数据(属性)之外,还包含着一个字符串:ASP.NET Home Page ,这是另外一种数据的存放方式。
另外,LinkGroup和LItem都允许重复出现,我们可以用数组或者列表(Array,List)来理解它们。
我还发现一些嵌套关系:LinkGroup可以包含Glyph,Context包含着Links,Links又包含了多个LItem。
不管如何嵌套,我发现数据都是包含在一个一个的XML节点中。
如果用专业的单词来描述它们,我们可以将ID,Title,Priority这三个数据项称为 XmlAttribute,LItem,LinkGroup节点称为 XmlElement,”ASP.NET Home Page“出现的位置可以称为 InnerText。基本上,XML就是由这三类数据组成。
下面我来演示如何使用这三种数据项。
回到顶部
首先,我来定义一个类型:
public class Class1{ public int IntValue { get; set; } public string StrValue { get; set; }}
下面是序列化与反序列的调用代码:
Class1 c1 = new Class1 { IntValue = 3, StrValue = "Fish Li" };string xml = XmlHelper.XmlSerialize(c1, Encoding.UTF8);Console.WriteLine(xml);Console.WriteLine("---------------------------------------");Class1 c2 = XmlHelper.XmlDeserialize<Class1>(xml, Encoding.UTF8);Console.WriteLine("IntValue: " + c2.IntValue.ToString());Console.WriteLine("StrValue: " + c2.StrValue);
运行结果如下:
<?xml version="1.0" encoding="utf-8"?><Class1 xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <IntValue>3</IntValue> <StrValue>Fish Li</StrValue></Class1>---------------------------------------IntValue: 3StrValue: Fish Li
结果显示,IntValue和StrValue这二个属性生成了XmlElement。
小结:默认情况下(不加任何Attribute),类型中的属性或者字段,都会生成XmlElement。
回到顶部
再来定义一个类型:
public class Class2{ [XmlAttribute] public int IntValue { get; set; } [XmlElement] public string StrValue { get; set; }}
注意,我在二个属性上增加的不同的Attribute.
下面是序列化与反序列的调用代码:
Class2 c1 = new Class2 { IntValue = 3, StrValue = "Fish Li" };string xml = XmlHelper.XmlSerialize(c1, Encoding.UTF8);Console.WriteLine(xml);Console.WriteLine("---------------------------------------");Class2 c2 = XmlHelper.XmlDeserialize<Class2>(xml, Encoding.UTF8);Console.WriteLine("IntValue: " + c2.IntValue.ToString());Console.WriteLine("StrValue: " + c2.StrValue);
运行结果如下(我将结果做了换行处理):
<?xml version="1.0" encoding="utf-8"?><Class2 xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema" IntValue="3"> <StrValue>Fish Li</StrValue></Class2>---------------------------------------IntValue: 3StrValue: Fish Li
结果显示:
1. IntValue 生成了XmlAttribute
2. StrValue 生成了XmlElement(和不加[XmlElement]的效果一样,表示就是默认行为)。
小结:如果希望类型中的属性或者字段生成XmlAttribute,需要在类型的成员上用[XmlAttribute]来指出。
回到顶部
还是来定义一个类型:
public class Class3{ [XmlAttribute] public int IntValue { get; set; } [XmlText] public string StrValue { get; set; }}
注意,我在StrValue上增加的不同的Attribute.
下面是序列化与反序列的调用代码:
Class3 c1 = new Class3 { IntValue = 3, StrValue = "Fish Li" };string xml = XmlHelper.XmlSerialize(c1, Encoding.UTF8);Console.WriteLine(xml);Console.WriteLine("---------------------------------------");Class3 c2 = XmlHelper.XmlDeserialize<Class3>(xml, Encoding.UTF8);Console.WriteLine("IntValue: " + c2.IntValue.ToString());Console.WriteLine("StrValue: " + c2.StrValue);
运行结果如下(我将结果做了换行处理):
<?xml version="1.0" encoding="utf-8"?><Class3 xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema" IntValue="3">Fish Li</Class3>---------------------------------------IntValue: 3StrValue: Fish Li
结果符合预期:StrValue属性在增加了[XmlText]之后,生成了一个文本节点(InnerText)
小结:如果希望类型中的属性或者字段生成InnerText,需要在类型的成员上用[XmlText]来指出。
回到顶部
看过前面几个示例,大家应该能发现:通过序列化得到的XmlElement和XmlAttribute都与类型的数据成员或者类型同名。然而有时候我们可以希望让属性名与XML的节点名称不一样,那么就要使用【重命名】的功能了,请看以下示例:
[XmlType("c4")]public class Class4{ [XmlAttribute("id")] public int IntValue { get; set; } [XmlElement("name")] public string StrValue { get; set; }}
序列化与反序列的调用代码前面已经多次看到,这里就省略它们了。
运行结果如下(我将结果做了换行处理):
<?xml version="1.0" encoding="utf-8"?><c4 xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema" id="3"> <name>Fish Li</name></c4>---------------------------------------IntValue: 3StrValue: Fish Li
看看输出结果中的红字粗体字,再看看类型定义中的三个Attribute的三个字符串参数,我想你能发现规律的。
小结:XmlAttribute,XmlElement允许接受一个别名用来控制生成节点的名称,类型的重命名用XmlType来实现。
回到顶部
继续看示例代码:
Class4 c1 = new Class4 { IntValue = 3, StrValue = "Fish Li" };Class4 c2 = new Class4 { IntValue = 4, StrValue = "//www.cnblogs.com/fish-li/" };// 说明:下面二行代码的输出结果是一样的。List<Class4> list = new List<Class4> { c1, c2 };//Class4[] list = new Class4[] { c1, c2 };string xml = XmlHelper.XmlSerialize(list, Encoding.UTF8);Console.WriteLine(xml);// 序列化的结果,反序列化一定能读取,所以就不再测试反序列化了。
运行结果如下:
<?xml version="1.0" encoding="utf-8"?><ArrayOfC4 xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <c4 id="3"> <name>Fish Li</name> </c4> <c4 id="4"> <name>//www.cnblogs.com/fish-li/</name> </c4></ArrayOfC4>
现在c4节点已经重复出现了,显然,是我们期待的结果。
不过,ArrayOfC4,这个节点名看起来太奇怪了,能不能给它也重命名呢?
继续看代码,我可以定义一个新的类型:
// 二种Attribute都可以完成同样的功能。//[XmlType("c4List")][XmlRoot("c4List")]public class Class4List : List<Class4> { }
然后,改一下调用代码:
Class4List list = new Class4List { c1, c2 };
运行结果如下:
<?xml version="1.0" encoding="utf-8"?><c4List xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <c4 id="3"> <name>Fish Li</name> </c4> <c4 id="4"> <name>//www.cnblogs.com/fish-li/</name> </c4></c4List>
小结:数组和列表都能直接序列化,如果要重命名根节点名称,需要创建一个新类型来实现。
回到顶部
首先,还是定义一个类型:
public class Root{ public Class3 Class3 { get; set; } public List<Class2> List { get; set; }}
序列化的调用代码:
Class2 c1 = new Class2 { IntValue = 3, StrValue = "Fish Li" };Class2 c2 = new Class2 { IntValue = 4, StrValue = "//www.cnblogs.com/fish-li/" };Class3 c3 = new Class3 { IntValue = 5, StrValue = "Test List" };Root root = new Root { Class3 = c3, List = new List<Class2> { c1, c2 } };string xml = XmlHelper.XmlSerialize(root, Encoding.UTF8);Console.WriteLine(xml);
运行结果如下:
<?xml version="1.0" encoding="utf-8"?><Root xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <Class3 IntValue="5">Test List</Class3> <List> <Class2 IntValue="3"> <StrValue>Fish Li</StrValue> </Class2> <Class2 IntValue="4"> <StrValue>//www.cnblogs.com/fish-li/</StrValue> </Class2> </List></Root>
假设这里需要为List和Class2的节点重命名,该怎么办呢?
如果继续使用前面介绍的方法,是行不通的。
下面的代码演示了如何重命名列表节点的名称:
public class Root{ public Class3 Class3 { get; set; } [XmlArrayItem("c2")] [XmlArray("cccccccccccc")] public List<Class2> List { get; set; }}
序列化的调用代码与前面完全一样,得到的输出结果如下:
<?xml version="1.0" encoding="utf-8"?><Root xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <Class3 IntValue="5">Test List</Class3> <cccccccccccc> <c2 IntValue="3"> <StrValue>Fish Li</StrValue> </c2> <c2 IntValue="4"> <StrValue>//www.cnblogs.com/fish-li/</StrValue> </c2> </cccccccccccc></Root>
想不想把cccccccccccc节点去掉呢(直接出现c2节点)?
下面的类型定义方式实现了这个想法:
public class Root{ public Class3 Class3 { get; set; } [XmlElement("c2")] public List<Class2> List { get; set; }}
输出结果如下:
<?xml version="1.0" encoding="utf-8"?><Root xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <Class3 IntValue="5">Test List</Class3> <c2 IntValue="3"> <StrValue>Fish Li</StrValue> </c2> <c2 IntValue="4"> <StrValue>//www.cnblogs.com/fish-li/</StrValue> </c2></Root>
小结:数组和列表都在序列化时,默认情况下会根据类型中的数据成员名称生成一个节点,列表项会生成子节点,如果要重命名,可以使用[XmlArrayItem]和[XmlArray]来实现。还可以直接用[XmlElement]控制不生成列表的父节点。
回到顶部
列表元素可以是同一种类型,也可以不是同一种类型(某个类型的派生类)。
例如下面的XML:
<?xml version="1.0" encoding="utf-8"?><XRoot xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:xsd="//www.w3.org/2001/XMLSchema"> <List> <x1 aa="1" bb="2" /> <x1 aa="3" bb="4" /> <x2> <cc>ccccccccccc</cc> <dd>dddddddddddd</dd> </x2> </List></XRoot>
想像一下,上面这段XML是通过什么类型得到的呢?
答案如下(注意红色粗体部分):
public class XBase { }[XmlType("x1")]public class X1 : XBase{ [XmlAttribute("aa")] &nb
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号