将Object转换为XDocment对象

代码如下:

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

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的更多相关文章

  1. Convert Object to XML using LINQ

    Convert Object to XML using LINQ. Also the object contains other object list. Following is the Class ...

  2. tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor

    最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...

  3. document.forms[0].submit object is not a function

    今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. ...

  4. java基础2.0:Object、Class、克隆、异常编程

    Java编程中两个重要的类Object 和 Class及java异常编程 (1)是所有Java类(API提供的类.自定义类)的最终父类. (2)作用:在JVM管理对象中的过程中,有一套统一的类型检查和 ...

  5. [Ramda] Convert Object Methods into Composable Functions with Ramda

    In this lesson, we'll look at how we can use Ramda's invoker and constructNfunctions to take methods ...

  6. bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value

    原文: https://feiffy.cc/uncaught-typeerror-cannot-convert-object-to-primitive-value 最近发现我的博客页面移动端上下拉菜单 ...

  7. C# XML技术总结之XDocument 和XmlDocument

    引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...

  8. c#学习<四>:C#2.0、C#3.0

    委托的演变 委托(C#1.0) 委托可看作是只定义了一个方法的接口,将委托的实例看作实现了这个接口的一个对象. 委托的执行要满足4个条件: 1. 声明委托类型                     ...

  9. RCE via XStream object deserialization && SECURITY-247 / CVE-2016-0792 XML reconstruction Object Code Inject

    catalogue . Java xStream . DynamicProxyConverter . java.beans.EventHandler . RCE via XStream object ...

随机推荐

  1. SQL server 变量、运算符

    一.三个表的练习 表一:学生表 student学号:code int (主键)从1开始姓名:name varchar(50)性别:sex char(10)班级:banji char(10)语文教师编号 ...

  2. Android 一个页面上下两个ListView的页面显示

    Android 一个页面上下两个ListView,当上面的ListView过长时,下面的List基本没有了滑动空间,查阅网上资料,解决办法基本是采用ScrollView做页面滑动,notifyData ...

  3. linux下USB串口,minicom

    [一].驱动相关说明: 如果直接使用串口线,而没有用到USB转串口设备,就不需要安装驱动. 如果使用了USB转串口,一般情况下也不需要安装驱动了,目前linux系统已经包含了该驱动,可以自动识别,亦可 ...

  4. Azure SQL 数据库的灵活缩放预览版简介

    Eron Kelly SQL Server 产品管理部门产品市场营销总经理 几天前,我们宣布了发布 Azure SQL 数据库的灵活缩放公共预览版.新增的灵活缩放功能通过简化开发和管理,简化了扩展和缩 ...

  5. (转载)Mac和iOS开发资源汇总—更新于2013-07-19

    (转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...

  6. 判断iis是否已经安装

    判断iis是否已经安装? 访问http://127.0.0.1 能得到正确页面的是已经安装. 活者查看控制面板-添加删除程序-windows组件-internet信息服务(IIS)前面的没有打勾则没有 ...

  7. Delphi 对象的创建(create)与释放(free/destory)

    Delphi 对象的创建(create)与释放(free/destory) 1.Create参数为:nil/self/application的区别,最好能看到实际效果的区别 例如: My := TMy ...

  8. 公有云开启全面竞争时代——青云QingCloud

    网界网本期[封面报道]公有云开启全面竞争时代 [CNW.com.cn 专稿] 险滩急流之后,公有云[注]服务市场的开拓者们终于看到了一片丰饶之海. 2013年,中国云计算[注]产业起步的第五年,公有云 ...

  9. Jquery 进度条集锦

    http://sc.chinaz.com/tag_jiaoben/JinDuTiao.html?qq-pf-to=pcqq.group

  10. C基础

    一.关于整型数据 1.整型常量:十进制数前面可以加+.-号,但是不能有前缀0 八进制数:必须以前缀0开头,不是O.不能加负号(-),否则不能识别. 十六进制数:前缀必须为0x或者0X.不能加负号(-) ...