码迷,mamicode.com
首页 > Web开发 > 详细

.Net 4.0 Convert Object to XDocument

时间:2014-05-12 04:11:31      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

 

将Object转换为XDocment对象

代码如下:

C# – Object to XDocument

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

VB.Net – Object to XDocument

bubuko.com,布布扣
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
bubuko.com,布布扣

 

.Net 4.0 Convert Object to XDocument,布布扣,bubuko.com

.Net 4.0 Convert Object to XDocument

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/CnKker/p/3721242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!