.Net 4.0 Convert Object to XDocument
2020-12-13 02:54
标签:des style blog class code java 将Object转换为XDocment对象 代码如下: .Net 4.0 Convert Object to XDocument,搜素材,soscw.com .Net 4.0 Convert Object to XDocument 标签:des style blog class code java 原文地址:http://www.cnblogs.com/CnKker/p/3721242.htmlC# – 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
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
文章标题:.Net 4.0 Convert Object to XDocument
文章链接:http://soscw.com/essay/26566.html