我是靠谱客的博主 敏感枫叶,这篇文章主要介绍详解 C# 中XML对象的序列化和反序列化,现在分享给大家,希望可以做个参考。

这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候能够自动去加载这些数据,由于我们的这些Model中字段众多,如果单独进行保存那是不太现实的,这个时候将这些字段序列化成xml字符串并保存在数据库中就是一个不错的选择,当我们需要这些数据的时候我们也可以反过来将其序列化为一些字段,最终达到我们的效果,首先我们来看看是如何实现的?

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class XMLUtil {     /// <summary>     /// XML & Datacontract Serialize & Deserialize Helper     /// </summary>     /// <typeparam name="T"></typeparam>     /// <param name="serialObject"></param>     /// <returns></returns>     public static string Serializer<T>(T serialObject) where T : class     {         try         {             XmlSerializer ser = new XmlSerializer(typeof(T));             System.IO.MemoryStream mem = new MemoryStream();             XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);             ser.Serialize(writer, serialObject);             writer.Close();               return Encoding.UTF8.GetString(mem.ToArray());         }         catch (Exception ex)         {             return null;         }     }       public static T Deserialize<T>(string str) where T : class     {         try         {             XmlSerializer mySerializer = new XmlSerializer(typeof(T));             StreamReader mem2 = new StreamReader(                     new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str)),                     System.Text.Encoding.UTF8);               return (T)mySerializer.Deserialize(mem2);         }         catch (Exception)         {             return null;         }     }      }

  微软为我们提供的XmlSerializer这个类就为我们提供了这个可能,在序列化的过程中我们需要注意下面的情况,所有的属性必须是可序列化的对象,像BitmapImage、SolidColorBrush等这些对象都是不能够进行序列化的对象,如果用上面的方法进行序列化时将返回null,正确的方式是在这些字段上面加上[XmlIgnore]说明,从而在进行序列化时候跳过这些对象,就像下面的方式。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary> ///背景颜色 /// </summary> private SolidColorBrush _BackgroundColor; [XmlIgnore] public SolidColorBrush BackgroundColor {     get     {         return _BackgroundColor;     }     set     {         if (value != _BackgroundColor)         {             _BackgroundColor = value;             OnPropertyChanged("BackgroundColor");         }     } }

  那么像上面的这个SolidColorBrush对象该怎样去进行序列化呢,这里我们选择将当前颜色的ARGB值保存在一个byte数组中,从而在反序列化的时候通过Color.FromArgb的方式进行还原,就像下面的方式。

复制代码
1
2
3
byte[] colorByte=savedModel.ConfigurationBaseModel.BackgroundColorArgb;     Color backColor=Color.FromArgb(colorByte[0],colorByte[1],colorByte[2],colorByte[3]);     sourceBaseModel.BackgroundColor = new SolidColorBrush(backColor);

  那么这个对象在进行序列化的时候该怎么进行保存呢?同样的原理我们可以通过下面的方式。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// <summary> ///背景颜色 /// </summary> private SolidColorBrush _BackgroundColor; [XmlIgnore] public SolidColorBrush BackgroundColor {     get     {         return _BackgroundColor;     }     set     {         if (value != _BackgroundColor)         {             _BackgroundColor = value;             OnPropertyChanged("BackgroundColor");         }     } }   /// <summary> ///背景颜色ARGB /// </summary> private byte[] _BackgroundColorArgb=new byte[4]; [XmlArray("argb"),XmlArrayItem("value")] public byte[] BackgroundColorArgb {     get     {         if (null != _BackgroundColor)         {             Color color = _BackgroundColor.Color;             _BackgroundColorArgb[0] = color.A;             _BackgroundColorArgb[1] = color.R;             _BackgroundColorArgb[2] = color.G;             _BackgroundColorArgb[3] = color.B;         }         return _BackgroundColorArgb;     }     set     {         if (value != _BackgroundColorArgb)         {             _BackgroundColorArgb = value;             OnPropertyChanged("BackgroundColorArgb");         }           } }

  这里在实际的使用中发现,就像byte数组必须通过[XmlArray("argb"),XmlArrayItem("value")] 这类型的标识来将其分类,在将其序列化完毕以后,我们可以看看这个BackgroundColorArgb字段最终是通过怎样的方式来保存的?   

    在进行反序列化的时候会将这个argb和value反序列化为一个byte数组。

    除了这些特例意外,有时候经常需要将一些对象的数组进行序列化,那么原理是什么呢?这里我借用别人的一个例子来进行相关的说明。

 对象数组的Xml序列化:

 数组的Xml序列化需要使用XmlArrayAttribute和XmlArrayItemAttribute;XmlArrayAttribute指定数组元素的Xml节点名,XmlArrayItemAttribute指定数组元素的Xml节点名。

 如下代码示例: 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization;    namespace UseXmlSerialization {     class Program     {         static void Main(string[] args)         {             //声明一个猫咪对象             var cWhite = new Cat { Color = "White", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };             var cBlack = new Cat { Color = "Black", Speed = 10, Saying = "White or black,  so long as the cat can catch mice,  it is a good cat" };                CatCollection cc = new CatCollection { Cats = new Cat[] { cWhite,cBlack} };                //序列化这个对象             XmlSerializer serializer = new XmlSerializer(typeof(CatCollection));                //将对象序列化输出到控制台             serializer.Serialize(Console.Out, cc);                Console.Read();         }     }        [XmlRoot("cats")]     public class CatCollection     {         [XmlArray("items"),XmlArrayItem("item")]         public Cat[] Cats { get; set; }     }        [XmlRoot("cat")]     public class Cat     {         //定义Color属性的序列化为cat节点的属性         [XmlAttribute("color")]         public string Color { get; set; }            //要求不序列化Speed属性         [XmlIgnore]         public int Speed { get; set; }            //设置Saying属性序列化为Xml子元素         [XmlElement("saying")]         public string Saying { get; set; }     } }

  最终获得的结果是:  

复制代码
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="gb2312"?> <cats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <items>     <item color="White">       <saying>White or black,  so long as the cat can catch mice,  it is a good cat</saying>     </item>     <item color="Black">       <saying>White or black,  so long as the cat can catch mice,  it is a good cat</saying>     </item>   </items> </cats>

以上就是详解 C# 中XML对象的序列化和反序列化的详细内容,更多关于c# xml序列化和反序列化的资料请关注靠谱客其它相关文章!

最后

以上就是敏感枫叶最近收集整理的关于详解 C# 中XML对象的序列化和反序列化的全部内容,更多相关详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部