Before we go from the network to help deal with a Xml class, and collate it, and help this class for serialization and de-serialization for Object type, without the need to define the structure Xml ahead of time, put it here for future use
1 /// <the Summary> 2 /// function: Xml serialization, deserialization helper classes 3 /// Description: 4 /// Created: 5 /// Created: March 13, 2014 6 / // </ Summary> . 7 public static class XmlHelper . 8 { . 9 /// <Summary> 10 /// private method is not a method to access the external 11 /// serialized object 12 is /// </ Summary> 13 is // / <param name = "stream"> flow </ param> 14 /// <param name = "O">对象</param> 15 /// <param name="encoding">编码方式</param> 16 private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding) 17 { 18 if (o == null) 19 throw new ArgumentNullException("o"); 20 if (encoding == null) 21 throw new ArgumentNullException("encoding"); 22 23 XmlSerializer serializer = new XmlSerializer(o.GetType()); 24 25 XmlWriterSettings settings = new XmlWriterSettings(); 26 settings.Indent = true; 27 settings.NewLineChars = "\r\n"; 28 settings.Encoding = encoding; 29 settings.IndentChars = " "; 30 31 using (XmlWriter writer = XmlWriter.Create(stream, settings)) 32 { 33 serializer.Serialize (Writer, O); 34 is writer.Close (); 35 } 36 } 37 [ 38 is /// <Summary> 39 /// sequence of the target sequence into a XML string 40 /// </ Summary> 41 is /// <param name = "O"> to serialize objects </ param> 42 is /// <param name = "encoding"> coding </ param> 43 is /// <Returns> serialized XML string generated </ Returns> 44 is public static string xmlserialize ( Object o, Encoding encoding) 45 { 46 using (MemoryStream stream = new MemoryStream()) 47 { 48 XmlSerializeInternal(stream, o, encoding); 49 50 stream.Position = 0; 51 using (StreamReader reader = new StreamReader(stream, encoding)) 52 { 53 return reader.ReadToEnd(); 54 } 55 } 56 } 57 58 /// <Summary> 59 /// serialize an object written in XML serialization manner to a file 60 /// </ Summary> 61 is /// <param name = "O"> to serialize objects < / param> 62 is /// <param name = "path"> save file path </ param> 63 is /// <param name = "encoding"> coding </ param> 64 public static void XmlSerializeToFile ( Object O, String path, encoding encoding) 65 { 66 IF ( String . IsNullOrEmpty(path)) 67 throw newArgumentNullException The ( " path " ); 68 69 the using (the FileStream File = new new the FileStream (path, FileMode.Create, FileAccess.Write)) 70 { 71 is XmlSerializeInternal (File, O, encoding); 72 } 73 is } 74 75 /// < Summary> 76 /// deserialization, deserialized from XML strings 77 /// </ Summary> 78 /// <typeParam name = "T"> results Object type </ typeParam> 79 /// <param name = "s">XML string containing the object</ param> 80 /// <param name = "encoding"> coding </ param> 81 /// <Returns> Object obtained deserialized </ Returns> 82 public static T XmlDeserialize <T> ( String S , encoding encoding) 83 { 84 IF ( String .IsNullOrEmpty (S)) 85 the throw new new ArgumentNullException The ( " S " ); 86 IF (encoding == null ) 87 the throw new new ArgumentNullException The ( "encoding"); 88 89 XmlSerializer mySerializer = new XmlSerializer(typeof(T)); 90 using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) 91 { 92 using (StreamReader sr = new StreamReader(ms, encoding)) 93 { 94 return (T)mySerializer.Deserialize(sr); 95 } 96 } 97 } 98 99 /// <summary> 100 ///Deserialization reads a file, press the XML way deserialized object. 101 /// </ Summary> 102 /// <typeParam name = "T"> Results Object Type </ typeParam> 103 /// <name = "path" param> File Path </ param> 104 /// < param name = "encoding"> coding </ param> 105 /// <Returns> Object obtained deserialized </ Returns> 106 public static T XmlDeserializeFromFile <T> ( String path, encoding encoding) 107 { 108 IF ( String . ArgumentNullException("path"); 110 if (encoding == null) 111 throw new ArgumentNullException("encoding"); 112 113 string xml = File.ReadAllText(path, encoding); 114 return XmlDeserialize<T>(xml, encoding); 115 } 116 }
Reproduced in: https: //www.cnblogs.com/OnlyVersion/p/4503742.html