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 ...
随机推荐
- 【BZOJ 2115】【WC 2011】Xor
计算1到n的一条路径使得路径上的值xor和最大. 先任意走一条路径计算xor和,然后dfs的时候处理出所有的环的xor和,这样对于所有的环的xor和求线性基,在任意走出的路径的xor和上贪心即可. 正 ...
- 64.GitHub 排名前100的android项目简介
GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...
- css选择器([class*=" icon-"], [class^=icon-] 的区别)
官方解释: [attribute^=value],a[src^="https"],选择其 src 属性值以 "https" 开头的每个 <a> 元素 ...
- SqlServerException:拒绝对表对象的select,insert权限解决(新建账号导致的问题)
继上一篇文章所述的问题,这次又出现了不能插入的问题.经过定位,也是由于我多选择了一个数据库用户角色的权限导致的,下面是详细的操作步骤 SqlServerException:拒绝了对对象 '...'(数 ...
- JSP文件下载
设置http响应头 response.setHeader 读取文件 这里需要注意两点: 读取的文件路径,必须设置为绝对路径 若文件名为中文,需要设置编码格式:URLEncoder.encode(fil ...
- Vmware vsphere 网络架构
VMware vSphere架构下服务器会虚拟出交换机来供ESX Host虚拟机来使用,虚拟交换机有两种,vSwitch虚拟交换机和vNetwork分布式虚拟交换机,每个ESX Host均有一个标准v ...
- [Android]GOF23种设计模式 & Android中的设计模式
GOF23种设计模式 设计原则: 1. 单一职责原则(SRP):就一个类而言,应该仅有一个引起它变化的原因 2. 开放-封闭原则(OCP):软件实体(类.模块.函数等)应该可以扩展,但是不可修改.即对 ...
- Mac下同时安装多个版本的JDK
JDK8 GA之后,小伙伴们喜大普奔,纷纷跃跃欲试,想体验一下Java8的Lambda等新特性,可是目前Java企业级应用的主打版本还是JDK6, JDK7.因此,我需要在我的电脑上同时有JDK8,J ...
- K米测试
K米评测 ------K米IOS4.3.0体验之旅 第一部分 :调研,评测 第一次上手体验: 像大多数同学一样,这也是我第一次使用k米这一类型的ktv点歌软件.我算是比较经常接触唱k的人,身边的朋友 ...
- 我所了解的JavaScript糟粕和鸡肋
糟粕 全局变量 众所周知,全局变量在很小的程序中可能会带来方便,但随着程序变得越来大,全局变量将难以处理,全局变量将降低程序的可靠性. 在js中有3种方式定义全局变量 脱离任何函数安排一个var语句 ...