将Object转换为XDocment对象
代码如下:
C# – Object to XDocument
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml.Linq; 6 using System.IO; 7 using System.Xml.Serialization; 8 using System.Xml; 9 10 namespace Utilities 11 { 12 public static class Serialization 13 { 14 15 public static T Deserialize<T>(StringReader dataIn) 16 { 17 return (T)(new XmlSerializer(typeof(T), String.Empty)).Deserialize(dataIn); 18 } 19 20 public static XDocument Serialize(object obj) 21 { 22 // Serialise to the XML document 23 var objectDocument = new XmlDocument(); 24 25 using (XmlWriter writer = (objectDocument.CreateNavigator()).AppendChild()) 26 { 27 (new XmlSerializer(obj.GetType())).Serialize(writer, obj); 28 writer.Close(); 29 } 30 31 return XDocument.Load(new XmlNodeReader(objectDocument)); 32 } 33 34 public static T Open<T>(string fileName) 35 { 36 if (String.IsNullOrEmpty(fileName)) 37 throw new ArgumentNullException("fileName"); 38 39 if (!File.Exists(fileName)) 40 throw new FileNotFoundException("The provided file does not exist.", fileName); 41 42 return (Serialization.Deserialize<T>(new StringReader(fileName))); 43 } 44 45 public static void Save(object obj, string fileName) 46 { 47 if (String.IsNullOrEmpty(fileName)) 48 throw new ArgumentNullException("fileName"); 49 50 if (!File.Exists(fileName) && !Directory.Exists(Path.GetFullPath(fileName)) && !(Directory.CreateDirectory(fileName)).Exists) 51 throw new DirectoryNotFoundException(String.Format("The provided Directory does not exist or cannot be created.")); 52 53 (Serialization.Serialize(obj)).Save(fileName); 54 } 55 } 56 }
VB.Net – Object to XDocument
Imports System.Collections.Generic Imports System.Linq Imports System.Text Imports System.Xml.Linq Imports System.IO Imports System.Xml.Serialization Imports System.Xml Namespace Utilities Public NotInheritable Class Serialization Private Sub New() End Sub Public Shared Function Deserialize(Of T)(dataIn As StringReader) As T Return DirectCast((New XmlSerializer(GetType(T), [String].Empty)).Deserialize(dataIn), T) End Function Public Shared Function Serialize(obj As Object) As XDocument ' Serialise to the XML document Dim objectDocument = New XmlDocument() Using writer As XmlWriter = (objectDocument.CreateNavigator()).AppendChild() (New XmlSerializer(obj.[GetType]())).Serialize(writer, obj) writer.Close() End Using Return XDocument.Load(New XmlNodeReader(objectDocument)) End Function Public Shared Function Open(Of T)(fileName As String) As T If [String].IsNullOrEmpty(fileName) Then Throw New ArgumentNullException("fileName") End If If Not File.Exists(fileName) Then Throw New FileNotFoundException("The provided file does not exist.", fileName) End If Return (Serialization.Deserialize(Of T)(New StringReader(fileName))) End Function Public Shared Sub Save(obj As Object, fileName As String) If [String].IsNullOrEmpty(fileName) Then Throw New ArgumentNullException("fileName") End If If Not File.Exists(fileName) AndAlso Not Directory.Exists(Path.GetFullPath(fileName)) AndAlso Not (Directory.CreateDirectory(fileName)).Exists Then Throw New DirectoryNotFoundException([String].Format("The provided Directory does not exist or cannot be created.")) End If (Serialization.Serialize(obj)).Save(fileName) End Sub End Class End Namespace