首页 > 技术文章 > c# XML-Object对象 序列化-反序列化

-wangjiannan 2014-01-14 15:45 原文

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Xaml;
  7 using System.Xml.Serialization;
  8 
  9 namespace BONC_BASE_Bonc_UI_Util_Xml
 10 {
 11     public class XmlUtil
 12     {
 13         /// <summary>
 14         /// to object from xml
 15         /// </summary>
 16         /// <param name="type"></param>
 17         /// <param name="xml"></param>
 18         /// <returns></returns>
 19         public static object Deserialize(Type type, string xml)
 20         {
 21             xml = ReplaceToType(xml);
 22             // Console.WriteLine(xml);
 23             try
 24             {
 25                 using (StringReader sr = new StringReader(xml))
 26                 {
 27                     XmlSerializer xmldes = new XmlSerializer(type);
 28                     return xmldes.Deserialize(sr);
 29                 }
 30             }
 31             catch (Exception e)
 32             {
 33                 Console.WriteLine("xml2Obj exception"+e.Message);
 34                 return null;
 35             }
 36         }
 37 
 38         /// <summary>  
 39         /// to xml from object
 40         /// </summary>  
 41         /// <param name="type"></param>  
 42         /// <param name="obj"></param>  
 43         /// <returns></returns>  
 44         public static string Serializer(Type type, Object obj)
 45         {
 46             MemoryStream Stream = new MemoryStream();
 47 
 48             XmlSerializer xml = new XmlSerializer(type);
 49             try
 50             {
 51                 xml.Serialize(Stream, obj);
 52             }
 53             catch (InvalidOperationException ioe)
 54             {
 55                 Console.WriteLine(ioe);
 56             }
 57             Stream.Position = 0;
 58             StreamReader sr = new StreamReader(Stream);
 59             string str = sr.ReadToEnd();
 60 
 61             //str = RepalceToClass(str);
 62 
 63             return str;
 64         }
 65 
 66         public static String RepalceToClass(String xml)
 67         {
 68             return xml.Replace("xsi:type", "class");
 69         }
 70 
 71         public static string RepalceToString(String xml)
 72         {
 73             return xml.Replace("xsd:string", "string");
 74         }
 75 
 76         private static String ReplaceToType(String xml)
 77         {
 78             return xml.Replace("<ReturnObject>",
 79                 "<ReturnObject  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
 80                 .Replace("class=", "xsi:type=");
 81         }
 82 
 83         public static string GetXmlJoint(string className, string innerXml)
 84         {
 85             string xmlJointTitle = "<?xml version=\"1.0\"?>\n";
 86             string xmlClassnameF =
 87                 "<" + className + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\n";
 88             string xmlClassnameE = "</" + className + ">";
 89 
 90             string xmlJoint = xmlJointTitle + xmlClassnameF + innerXml + xmlClassnameE;
 91             return xmlJoint;
 92         }
 93 
 94         public static string ReplaceList(string xml, String clazzName)
 95         {
 96             return xml.Replace("List", "ArrayOf"+clazzName);
 97         }
 98 
 99     }
100 }

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Windows.Forms;
 4 using System.Xml.Serialization;
 5 using BONC_BASE_Bonc_UI_Util_Xml;
 6 using NUnit.Framework;
 7 
 8 namespace BDIC_BASE.Bonc.UI.Util.Xml
 9 {
10     public class XMLTest
11     {
12         [Test]
13         public static void Test()
14         {
15             Name1 name = new Name1();
16             name.Id = "1";
17 
18             List<Name1> names = new List<Name1>();
19             names.Add(name);
20             names.Add(name);
21 
22             Person person = new Person();
23             person.Names = names;
24             person.Obj = names;
25 
26             // Serialize 
27             string xml = XmlUtil.Serializer(typeof(Person), person);
28             MessageBox.Show(xml);
29 
30             Person p = (Person)XmlUtil.Deserialize(typeof(Person), xml);
31             MessageBox.Show(p.Names[0].Id);
32         }
33     }
34 
35     public class Person
36     {
37         public List<Name1> Names { get; set; }
38 
39         [XmlElement(Namespace = "")]
40         public Object Obj = new Object();
41     }
42 
43     public class Name1
44     {
45         public String Id { get; set; }
46     }
47 }

 

 

推荐阅读