- 浏览: 399798 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
XDocument and its relative class XElement are core classes of the Linq XML in .net 4.0.
how to convert to and from data object with XElement is something that should be on the common knowledge to the C# developers.
here we will discuss with an example.
Suppose that we have a StudentInfo class, which is something like this :
[Serializable] public class StudentInfo { [XmlElement("Name")] public virtual string Name { get; set; } [XmlElement("Age")] public virtual int Age { get; set; } }
To Convert one data object to XDocument, you may do the following.
public StudentInfo ConvertFromXDocument(XDocument state_) { var studentInfo = state_.Element("StudentInfo"); var serializer = new XmlSerializer(typeof(StudentInfo)); var student = serializer.Deserialize(studentInfo.CreateReader()) as StudentInfo); }
Which basically that you use the XmlSerializer and you call the Deserialize method with XElement.CreateReader() method
To serialize one data object to XDocument, you can do the following.
public XDocument ConvertToXDocument(StudentInfo student) { using (var memoryStream = new MemoryStream()) { using (TextWriter streamwriter = new StreamWriter(memoryStream)) { var serializer = new XmlSerializer(typeof(StudentInfo)); serializer.Serialize(streamwriter, student); var xelement = XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())); return new XDocument(xelement); } } }
Basically what it does is to use the MemoryStream as intermediate medium, which you first do is to Seralize to the MemoryStream with StreamWriter... by calling the XmlSerializer.Serialize method. and later you parse the content returned by the MemoryStream, and just by using the XElement.Parse method to return the XElement.
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1976first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1925Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6551Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12039标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1244DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2573I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10946In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1287If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 938When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 813I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1918You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5330As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1185Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1712P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6091NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1221It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1091WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 867In WCF, primitives type are s ...
相关推荐
标题中的"A class used to serialize object into a database field"是指一种用于将对象序列化到数据库字段的类。在编程中,序列化是将对象的状态转换为可存储或传输的形式的过程,通常是为了持久化数据或者在网络...
题目中提到的"serialize form fields into an object"指的是将表单数据转换为JavaScript对象。虽然jQuery核心库没有提供直接将表单数据转换为对象的方法,但可以通过扩展jQuery来实现这个功能。通常,开发者会编写...
Serialize-javascript 能够序列化 JavaScript 库成含有正则表达式和功能的 JSON 包。示例:var serialize = require('serialize-javascript'); serialize({ str : 'string', num : 0, obj : {foo: ...
1. **序列化**:类可能会有一个`Serialize`方法,用于将对象转换成字节数组。这个过程通常涉及遍历对象的所有成员,将它们的值转化为字节并添加到数组中。为了实现这一点,对象通常需要支持某种形式的访问器(getter...
Comes with a propertyBag serializer, serialize any persistant VB object to/from a SOAP complex type out of the box with no extra code !. Support for both 1999 & 2001 Schema versions. Efficient stream ...
import serialize from 'axios-url-form-serialize'; const instance = axios.create(); instance.interceptors.request.use(config => { if (config.method === 'post' && config.data instanceof FormData) {...
前几天用LayUI表单进行AJAX提交的时候发现,function(data)里的data始终无法获取表单里填的值,当时我认为是出BUG了就用了$(‘#updateform’).serialize()来获取表单数据 //form表单 更新员工信息 form.on('...
受dominicbarnes / form-serialize的大力启发,但可与npm一起使用,并且本身不包含transform功能。 用法 serialize ( form ) == // {username: 'user1', password: 'my password'} < input name =' username ...
java8 看不到源码歌海森 注意:解码时,java 版本的 hessian 会默认跳过并忽略不存在的字段。 从 v1.6.0 版本开始,dubbo-go-hessian2 也会跳过不存在的字段,而 v1.6.0 之前的版本会返回错误。...时间....git
Most JSON serializers mimic object serialization libraries and try to serialize the entire object graph from the object being turned into JSON. This causes problems when you want a connected object ...
C#实现object与byte[]互转 在C#中,实现object与byte[]互转是一种常见的需求,特别是在网络通信和数据存储中。这篇文章将详细介绍如何将object转换为byte[],并介绍相关的知识点。 序列化 序列化是将对象转换为二...
XDocument doc = new XDocument(new XElement("Root", new XElement("Child1", "Value1"))); // 保存到文件 doc.Save("output.xml"); ``` 四、查询XML C#中的LINQ to XML提供了一种强大且直观的方式来查询XML数据...
大家知道Jquery中有serialize方法,可以将表单序列化为一个“&”连接的字符串,但却没有提供序列化为Json的方法。不过,我们可以写一个插件实现。 我在网上看到有人用替换的方法,先用serialize序列化后,将&替换...
public Node(Object element) { this.element_ = element; this.children_ = new Node[0]; // 初始化为空数组 } public void addChild(Node child) { Node[] newChildren = new Node[children_.length + 1]; ...
在MATLAB中,`serialize`函数是一个非常实用的工具,用于将MATLAB的数据对象编码为字符串或二进制格式,以便存储、传输或序列化。这个功能在开发过程中非常重要,尤其是在需要保存工作进度、跨程序传递数据或者进行...
本文实例讲述了jQuery基于ajax()使用serialize()提交form数据的方法。分享给大家供大家参考,具体如下: jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也...
XDocument xmlFromJson = XDocument.Parse(serializer.Serialize(deserializedObject)); ``` ### 五、读写JSON数据 在给定的`读写Json数据.txt`文件中,可能包含了具体的JSON读写操作。通常,我们可以通过以下方式...
连载 将表单数据序列化为查询字符串,数组,对象或JSON字符串。 安装 脚本标签 ... script src =" path/to/serialize.min.js " > </ script > ES模块 序列化还使用ES模块import语法支持现代浏览器
var template = tempistry.register(function() { /** function string provided from server-side tempistry.serialize() call*/}); // mixin pre/post render logic tempistry.on('pre-render...
在TIA博途中,序列化指令`Serialize`是一个非常重要的功能,它允许我们将复杂的自定义数据类型(如UDT, STRUCT等)转换为基本数据类型,通常是BYTE或CHAR类型的数组。这在数据交换、存储和通信过程中特别有用,因为...