.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 ...
随机推荐
- mysql主从配置(清晰的思路)
mysql主从配置.鄙人是在如下环境测试的: 主数据库所在的操作系统:win7 主数据库的版本:5.0 主数据库的ip地址:192.168.1.111 从数据库所在的操作系统:linux 从数据的版本 ...
- Java实现生产者消费者
方法1:最简单--利用LinkedBlockingQueue 队列具有先进先出的特点,成为经常应用于生产-消费者模式的数据结构. 1.将一个对象放到队列尾部,如果队列已满,就等待直到有空闲节点. —— ...
- poj1637
混合图欧拉回路首先先明确基本概念连通的无向图存在欧拉回路当且仅当不存在奇点连通的有向图当且仅当每个点入度=出度这道题我们显然应该当作连通的有向图来做这个问题的困难之处在于我不知道应该从无向边的什么方向 ...
- 如何查看jar包的版本号?
jar包根目录里的META-INF目录下的MANIFEST.MF文件里一般有会记录版本信息,可以到这个文件里查看 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录,这个 ...
- (转载)vsftpd简易配置
(转载)http://licong.blog.51cto.com/542131/145748/ 写篇关于vsftp配置的文章,加深自己的记忆,便于自己查阅,同时也希望能给其他需要的朋友一点借鉴.本文如 ...
- HDOJ 2011 多项式求和
Problem Description 多项式的描述如下: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + - 现在请你求出该多项式的前n项的和. Input 输入数据由2行组成, ...
- mysql数据库还原出错ERROR:Unknown command ‘\\’解决手记
使用mysql命令行客户端,使用source导入备份文件,但导入中出错, ERROR: Unknown command ‘\\’. ERROR: Unknown command ‘\”. ERROR: ...
- 逆转字符串leetcode
public class Solution { public String reverseWords(String s) { String ans=reverse(s); String s2[]=an ...
- HDU-1300(基础方程DP-遍历之前所有状态)
Problem Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, p ...
- CentOS系统cobbler完全部署及案例测试
本篇内容注主要涉及cobbler架构快速搭建,Cobbler命令行语法简单应用,Cobbler-WEB,system-config-kickStart基于Windows界面配置生成ks脚本模板,ks简 ...