.Net 4.0 Convert Object to XDocument
将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的更多相关文章
- Convert Object to XML using LINQ
Convert Object to XML using LINQ. Also the object contains other object list. Following is the Class ...
- tensorflow基础--LeNet-5测试模型遇到TypeError: Failed to convert object of type <class 'list'> to Tensor
最近在看<TensorFlow 实战Google深度学习框架第二版>这本书,测试LeNet-5这个模型时遇到了TypeError: Failed to convert object of ...
- document.forms[0].submit object is not a function
今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. ...
- java基础2.0:Object、Class、克隆、异常编程
Java编程中两个重要的类Object 和 Class及java异常编程 (1)是所有Java类(API提供的类.自定义类)的最终父类. (2)作用:在JVM管理对象中的过程中,有一套统一的类型检查和 ...
- [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 ...
- bootstrap导航条报错 Uncaught TypeError: Cannot convert object to primitive value
原文: https://feiffy.cc/uncaught-typeerror-cannot-convert-object-to-primitive-value 最近发现我的博客页面移动端上下拉菜单 ...
- C# XML技术总结之XDocument 和XmlDocument
引言 虽然现在Json在我们的数据交换中越来越成熟,但XML格式的数据还有很重要的地位. C#中对XML的处理也不断优化,那么我们如何选择XML的这几款处理类 XmlReader,XDocument ...
- c#学习<四>:C#2.0、C#3.0
委托的演变 委托(C#1.0) 委托可看作是只定义了一个方法的接口,将委托的实例看作实现了这个接口的一个对象. 委托的执行要满足4个条件: 1. 声明委托类型 ...
- 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 ...
随机推荐
- Git入门简介
1. Git 背景 Git 最初由Linus Torvalds编写,用于 Linux 内核开发的版本控制工具. Git 与常用的版本控制工具 CVS.Subversion 等不同,它采用了分布式版本 ...
- 【HDOJ】1150 Machine Schedule
匈牙利算法. #include <stdio.h> #include <string.h> #define MAXNUM 1005 char map[MAXNUM][MAXNU ...
- Delphi安装/卸载OCX控件的方法
delphi 安装卸载ocx 请参见 如下 http://blog.csdn.net/xt_chaoji/article/details/7027298 打开Delphi,关闭所有项目. 1. ...
- QTP自动化测试权威指南 连载(一)
第一章 简介 什么是自动化测试 自动化测试是对一个已有的手工测试过程减少并尽可能排除人工干预的过程. 什么时候适合做自动化测试 下面是一组适合将手工测试自动化的考量因素: ● 测试需要经常重复. ● ...
- 通过UIImagePickerController完成照相和相片的选取
UIImagePickerController是用于选取现有照片,或者用照相机现场照一张相片使用的 定义: @interface ShowViewController : UIViewControll ...
- AXI总线
AXI(Advanced eXtensible Interface)是一种总协议,该协议是ARM公司提出的AMBA(Advanced Microcontroller Bus Architecture) ...
- SRM 388(1-250pt)
题意:定义一个数为k-smooth,如果它最大的质因子不超过k.给定n和k,求不超过n的,k-smooth的数有多少个.(k <= 100, n <= 10^5) 解法:对于一个数t,判断 ...
- xapian倒排索引的归并流程
Xapian的检索流程和大部分搜索系统都一样,就先从倒排表抽取候选文档,然后结合其他信息进行排序,取top文档作为搜索结果,具体流程如下: 图1 xapian搜索流程 具体流程 在terms中找到do ...
- 最全面的 DNS 原理入门
DNS 是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 本文详细介绍DNS的原理,以及如何运用工具软件观察它的运作.我的目标是,读完此文后,你就能完全理解DNS. 一.D ...
- 关于Cookie的有关内容
1.首先谈谈http协议与状态保持. Http协议本身是无状态的,这与http协议本来的目的是相符的,客户端只需要简单的向服务器发出请求操作,然后服务器对请求作出响应.无论客户端还是服务器都没有必要记 ...