.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

.Net 4.0 Convert Object to XDocument

时间: 2024-11-07 00:24:23

.Net 4.0 Convert Object to XDocument的相关文章

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

今天在做项目的时候发现了一个问题:document.forms[0].submit object is not a function. 这个问题是在用JavaScript 代码来提交一个表单时发生的. <form action=".action" method="post"> <input type="text" name="member.name"/> <input type="but

[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 of an object and turn them into reusable utility functions that are curried and accept their object as the last argument. We'll convert a dot-chained

Android5.0框架层短信接收过程分析

本文分析使用的是android5.0的源代码,涉及的相关文件: frameworks\opt\telephony\src\java\com\android\internal\telephony\RIL.java frameworks\base\core\java\com\android\internal\util\StateMachine.java frameworks\opt\telephony\src\java\com\android\internal\telephony\InboundSm

Object.prototype.valueOf()和Object.prototype.toString()

valueOf() 方法用来把对象转换成原始类型的值(数值.字符串和布尔值) toString() 方法返回一个表示该对象的字符串. 原始类型 Number String Boolean Undefined Null String类型转换 如果 toString 方法存在并且返回原始类型,返回 toString 的结果. 如果 toString 方法不存在或者返回的不是原始类型,调用 valueOf 方法,如果 valueOf 方法存在,并且返回原始类型数据,返回 valueOf 的结果. 其他

WPF DataGrid 列显示0,-1(作废、删除)状态,1,2(支出、收入)类型,操作人(在其他表中),如何转换格式。

操作人,左联,Join on letf //容我补充 状态,类型,类似的转换,在xmlns中引入common   xmlns:com="clr-namespace:XXX.Common", 在<UserControl.Resources>中 xkey Binding  StatusConvert(状态转换) 也就是 XAML 中 需要转换的列 binding的 Converter={StaticResource StatusConvert} 在common中,CConver

windows phone 8.0 app 移植到windows10 app笔记

8.0 public class Convisibility : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { } public object ConvertBack(object value, Type targetType, object parameter, System.

【C#公共帮助类】 Convert帮助类

大家知道,开发项目除了数据访问层很重要外,就是Common了,这里就提供了强大且实用的工具. [C#公共帮助类] Image帮助类 一.DataTable using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Reflection; using System.Collections; namespace Common

WCF4.0 –- RESTful WCF Services

转自:http://blog.csdn.net/fangxinggood/article/details/6235662 WCF 很好的支持了 REST 的开发, 而 RESTful 的服务通常是架构层面上的考虑. 因为它天生就具有很好的跨平台跨语言的集成能力,几乎所有的语言和网络平台都支持 HTTP 请求,无需去实现复杂的客户端代理,无需使用复杂的数据通讯方式既可以将我们的服务暴露给任何需要的人,无论他使用 VB.Ruby.JavaScript,甚至是 HTML FORM,或者直接在浏览器地址

Convert和RelativeSource

自定义Converter 后台Converter类实现接口IValueConverter方法Convert是值->UI方法ConvertBack是UI->值初始化走Convert public class ColorConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { //value为当前的对象 var i