zoukankan      html  css  js  c++  java
  • .Net 4.0 Convert Object to XDocument

    将Object转换为XDocment对象

    代码如下:

    C# – 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<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 }

     

    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
  • 相关阅读:
    IOS7 UI设计的十大准则
    IOS8-人机界面指南
    Android应用切换皮肤功能实现
    Android 打造自己的个性化应用(五):仿墨迹天气实现续--> 使用Ant实现zip/tar的压缩与解压
    Android 打造自己的个性化应用(四):仿墨迹天气实现-->自定义扩展名的zip格式的皮肤
    Android 打造自己的个性化应用(三):应用程序的插件化
    Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
    Android 打造自己的个性化应用(一):应用程序换肤主流方式的分析与概述
    Android防止内存泄漏以及MAT的使用
    Android内存泄漏监测(MAT)及解决办法
  • 原文地址:https://www.cnblogs.com/CnKker/p/3721242.html
Copyright © 2011-2022 走看看