I wrote up a quick sample application that converts .Net objects into XDocuments using XmlSerializer. This code is not perfect and I can imagine that objects that contain circular references will blow up. DataContractSerialization may be a better option for that. More information on de/serialization at Microsoft.
For converting XmlDocument to XDocument, I used some of the suggestions from Marcelo Lopez Ruiz’s blog post Fast way to convert XmlDocument into XDocument.
C# – Object to XDocument
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace Utilities
{
public static class Serialization
{
public static T Deserialize<T>(StringReader dataIn)
{
return (T)(new XmlSerializer(typeof(T), String.Empty)).Deserialize(dataIn);
}
public static XDocument Serialize(object obj)
{
// Serialise to the XML document
var objectDocument = new XmlDocument();
using (XmlWriter writer = (objectDocument.CreateNavigator()).AppendChild())
{
(new XmlSerializer(obj.GetType())).Serialize(writer, obj);
writer.Close();
}
return XDocument.Load(new XmlNodeReader(objectDocument));
}
public static T Open<T>(string fileName)
{
if (String.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
if (!File.Exists(fileName))
throw new FileNotFoundException("The provided file does not exist.", fileName);
return (Serialization.Deserialize<T>(new StringReader(fileName)));
}
public static void Save(object obj, string fileName)
{
if (String.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
if (!File.Exists(fileName) && !Directory.Exists(Path.GetFullPath(fileName)) && !(Directory.CreateDirectory(fileName)).Exists)
throw new DirectoryNotFoundException(String.Format("The provided Directory does not exist or cannot be created."));
(Serialization.Serialize(obj)).Save(fileName);
}
}
}
VB.Net – Object to XDocument
(Note: This code was generated from a converter and has not been tested.)
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