Convert object to Xdocment object
The code is as follows:
C#–object to XDocument
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Xml.Linq;6 usingSystem.IO;7 usingSystem.Xml.Serialization;8 usingSystem.Xml;9 Ten namespaceUtilities One { A Public Static classSerialization - { - the Public StaticT deserialize<t>(StringReader datain) - { - returnT (NewXmlSerializer (typeof(T), String.Empty)). Deserialize (datain); - } + - Public StaticXDocument Serialize (Objectobj) + { A //serialise to the XML document at varObjectdocument =NewXmlDocument (); - - using(XmlWriter writer =(Objectdocument.createnavigator ()). AppendChild ()) - { -(NewXmlSerializer (obj. GetType ())). Serialize (writer, obj); - writer. Close (); in } - to returnXdocument.load (NewXmlNodeReader (objectdocument)); + } - the Public StaticT open<t> (stringfileName) * { $ if(String.IsNullOrEmpty (fileName))Panax Notoginseng Throw NewArgumentNullException ("FileName"); - the if(!file.exists (fileName)) + Throw NewFileNotFoundException ("The provided file does not exist.", fileName); A the return(Serialization.deserialize<t> (NewStringReader (FileName)); + } - $ Public Static voidSave (ObjectObjstringfileName) $ { - if(String.IsNullOrEmpty (fileName)) - Throw NewArgumentNullException ("FileName"); the - if(! File.exists (fileName) &&! Directory.Exists (Path.GetFullPath (fileName)) &&!(Directory.CreateDirectory (FileName)). Exists)Wuyi Throw NewDirectoryNotFoundException (String.Format ("The provided Directory does not exist or cannot be created.")); the - (serialization.serialize (obj)). Save (fileName); Wu } - } About}
Vb.net–object to XDocument
Imports System.Collections.GenericImports system.linqimports system.textimports System.Xml.LinqImports System.ioimports System.Xml.SerializationImports system.xmlnamespace Utilities public NotInheritable Class Serialization Private Sub New () End Sub public Shared Function deserialize (of T) (DataIn as Stringread ER) 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 documentDim 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 S hared 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)) A Ndalso Not (Directory.CreateDirectory (fileName)). Exists then Throw the New directorynotfoundexception ([String]. Format ("The provided Directory does not exist or cannot be created.") ) End If (serialization.serialize (obj)). Save (fileName) End Sub end Classend Namespace