. Net 4.0 Convert Object to XDocument

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.