将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 能做什么?

    SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 SQL 可更新数据库中的数据 SQL 可从数据库删除记录 SQL 可创建新数据库 SQL ...

  2. Android SectionIndexer 的使用(联系人分类索引)

    // 获取标题栏索引 int position = sectionIndexter.getPositionForSection(l[idx]); ) { return true; } // 设置调整到 ...

  3. android工程混淆和反编译

    一.工程文件的混淆 混淆文件下载:http://download.csdn.net/detail/lxq_xsyu/6328751 1.在根目录下添加progard.cfg文件 2.打开project ...

  4. bzoj1455

    学习了一下可合并堆的一种写法——左偏树感觉左偏树是一种类似启发式的方法学习左偏树后这题就水过去了 ..] of longint; v:..] of boolean; i,n,m,x,y,f:longi ...

  5. 在ASP.NET MVC中对手机号码的验证

    在ASP.NET MVC中,可以使用RegularExpression特性来验证手机号码. public class Customer { [Required(ErrorMessage = " ...

  6. (转载)JavaScript中定义变量

    (转载)http://blog.163.com/xuxiaoqianhz@126/blog/static/165190577201061594421870/ JavaScript中定义变量有两种方式: ...

  7. 小情人emacs的自动补全

    前天打字的时候发现手指疼-..OTL-思考了一下可能是我近几个月以来一直在使用全部手敲代码不使用自动补全的"恶果"(当然我还是建议全部手敲的,至少可以感觉到强烈的屌丝满足感). 先 ...

  8. 数据分析:Weka,Matlab,R,SPSS,SAS等分析软件的入门

    1 功能角度 weka是机器学习方面的工具(开源).spss是数学工具(商业工具). 具体的说,weka的主要功能是模式分类,或者模式识别或者回归.包括特征的降维(PCA),特征选择,训练模型以及对测 ...

  9. Oracle中trunc函数、round 函数、ceil函数和floor函数的使用

    Oracle中trunc函数.round 函数.ceil函数和floor函数的使用 1.1trunc函数处理数字 trunc函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定 ...

  10. IDEA新建SpringMVC项目报错解决办法

    网页运行的错误: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundE ...