Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization
More info here: http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserialization-entity-classes-with-xsd-exe.aspx
Introduction
Many time we come across the need of parsing XML document and creating entities. These entities are later passed between multiple methods, stored as configuration or used to perform some manipulation. Hand wrting code for parsing these entities from XML document is tedious. This article presents and easy way to parse XML documents and generating deserializers.
Hands on!
Lets create a sample XML document named Students.xml following is the sample content.
<xml version="1.0" encoding="utf-8" ?>
<Students>
<Student>
<RollNo>1</RollNo>
<Name>Student 1</Name>
<Address>Xyz Street</Address>
</Student>
<Student>
<RollNo>2</RollNo>
<Name>Student 2</Name>
<Address>Xyz Street</Address>
</Student>
<Student>
<RollNo>3</RollNo>
<Name>Student 3</Name>
<Address>Xyz Street</Address>
</Student>
<Student>
<RollNo>4</RollNo>
<Name>Student 4</Name>
<Address>Xyz Street</Address>
</Student>
<Student>
<RollNo>5</RollNo>
<Name>Student 5</Name>
<Address>Xyz Street</Address>
</Student>
</Students>
Here, as we can see, there is one root node named <Students> and it contains <Student> nodes as children. Now, what if we need to create a list of students in our program and bind it to some grid? We have to write manual parsing code for achieving same. But, you can easily achieve this task using xsd.exe to generate de-serializer and get the student collection. Following are the steps
- Start Visual Studio and open Student.xml
- Click on "XML" -> "Create Schema" on toolbar
- It will generate XML Schema. Following is the sample schema generated for Students.xml file
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Students">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Student">
<xs:complexType>
<xs:sequence>
<xs:element name="RollNo" type="xs:unsignedByte" />
<xs:element name="Name" type="xs:string" />
<xs:element name="Address" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> - Here we can observe the datatypes are automatically set as per the data available in Student.xml As per your need you can edit the schema and set the appropriate data types.
- Save the Students.xml and Students.xsd files inside some folder say "C:\Sample\XML"
- Now, we have to start "Visual Studio Command Prompt"
- On VS Command Prompt, cd to "C:\Sample\XML" directory and type "xsd Students.xsd /c"
- This will generate "Students.cs" file that we can use to de-serialize Student.xml file
- Now create a "Console Application" named "XmlDeSerializer" we will use this to test the de-serialization
- Add "Students.cs" file that we have created using xsd.exe file
- Copy and paste following code in Program.cs file and run the program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization; namespace XmlDeSerializer
{
class Program
{
static void Main(string[] args)
{
using (FileStream xmlStream = new FileStream("C:\\Sample\\XML\\Students.xml", FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(Students));
Students deserializedStudents = serializer.Deserialize(xmlReader) as Students;
foreach (var student in deserializedStudents.Student)
{
Console.WriteLine("Roll No : {0}", student.RollNo);
Console.WriteLine("Name : {0}", student.Name);
Console.WriteLine("Address : {0}", student.Address);
Console.WriteLine("");
}
}
}
}
}
} You will see following output
In this way we have successfully de-serialized Students.xml file and created entities. You May also serialize existing entites back in the xml format, we can use "Serialize" method of XmlSerializer.
Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization的更多相关文章
- 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD
利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD 1, 命令提示符-->找到vs自带的xsd.exe工具所在的文件夹 例如: C:\Program Files ...
- xsd.exe的使用
xsd.exe的使用 XML文件生成XSD文件 xsd myFile.xml /outputdir:myOutputDir XSD文件生存实体类 xsd <你的XSD路径>.xsd / ...
- 使用 xsd.exe 命令工具将 xsd 架构生成 类(CS) 文件
vs自带命令行工具 命令:xsd xml文件路径 C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>xsd d:Scheme.xml ...
- 如何由XSD自动生成XML和实体类
项目中有时候要用XML作为数据源,因此需要定义XML文件和相应的类,最佳方法是首先定义XSD,然后自动生成实体类,最后生成XML和填充数据:读取XML数据源的时候,首先用XSD验证XML数据格式,然后 ...
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...
- C# 使用xsd文件验证XML 格式是否正确
C# 使用xsd文件验证XML 格式是否正确 核心示例代码: //创建xmlDocument XmlDocument doc = new XmlDocument(); //创建声明段 如<?xm ...
- 28.XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
转自https://www.cnblogs.com/gdjlc/archive/2013/09/08/3308229.html XML Schema 语言也称作 XML Schema 定义(XML S ...
- DELPHI中调用XSD去验证XML的合法性
procedure TFrmPrintReport.Button3Click(Sender: TObject);var SchemaDoc, XmlDoc: IXMLDOMDocument2; S ...
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
今天配置Spring的xml出现了错误 Multiple annotations found at this line: - schema_reference.4: Failed to read sc ...
随机推荐
- ES6 变量的解构赋值
数组的解构赋值 var [a,b,c] = [1,2,3]; 左边是变量,右边是值,根据数据结构一一对应 只要等号两边的模式相同,左边的变量就会被赋予右边对应的值,必须模式相同 如果等号 ...
- 网页端压缩解压缩插件JSZIP库的使用
JSZIP这个库支持在网页端生成zip格式的文件, 官方网站是:http://stuk.github.io/jszip/ 官方网站的DEMO如下: <!DOCTYPE html> < ...
- <UL>中<li>标签前编号图片的简单调用
<style type="text/css"> ul li{ list-style-type:none} .men ul{ background:url(http:// ...
- jquery-ui-处理拖动位置Droppable,Draggable
一.效果.如下图中,各途中可相互拖拉,右下角可删除.注意图1和图2对比区别 图1 图2 二.源码详解 html源码 <!DOCTYPE html> <html> <hea ...
- .VDI manual Technical Logistics - Volume 2: Industrial Trucks
VDI manual Technical Logistics - Volume 2: Industrial Trucks Name Publication date: State VDI 2196 B ...
- js学习笔记1---使用方法
1.获取对象: a) 通过名称来获取元素:document.getElementById() //属于静态方法,方法前面只能用document b) document.getElementsByCla ...
- 【CodeForces 699A】Launch of Collider
维护最新的R,遇到L时如果R出现过就更新答案. #include <cstdio> #include <algorithm> using namespace std; int ...
- Xcode插件VVDocumenter Alcatraz KSImageNamed等安装
今天安装VVDocumenter,总是不起作用...所以用Alcatraz...下面介绍下Alcatraz 一.Alcatraz Alcatraz 是一款 Xcode的插件管理工具,可以用来管理XCo ...
- MAC下Eclipse的常用快捷键
整理Eclipse常用快捷键 开发环境切换到Mac下后原来Window下的快捷键很大一部分是不相容的,习惯了快捷键的生活忽然哪天快捷键不起作用了,跟着的就是开发效率明显降低,频繁录入错误的快捷键让Ec ...
- [转]Ajax跨域请求
一.编一个服务器端servlet @RequestMapping("/haha") @ResponseBody String haha(String haha, HttpServl ...