.NET : 如何动态根据一个业务实体类型创建XSD架构文件
这是正在开发的XML数据库的一个功能,我需要动态根据一个业务实体类型创建一个XSD架构文件,并使用其对最后的XML数据文件进行约束。
目前该功能仅仅是一个原型,还有待细化改进。例如在实体的成员上用一些特定的Attribute标明该成员要被保存的形态。
第一部分:业务实体类
(作为演示目的,我将所有的类型定义在一个文件里,同时每个类都只有少量简单的属性成员)
此处特别注意的是,Order这个类是很复杂的,它包含了一系列的OrderItem,而OrderItem又包含了Product对象。
using System;
using System.Collections.Generic;
using System.Text; namespace DataEntities
{public class Order{public int OrderID { get; set; }public string CustomerID { get; set; }public int EmployeeID { get; set; }public DateTime OrderDate { get; set; }public List<OrderItem> OrderItems { get; set; } public override string ToString(){StringBuilder sb = new StringBuilder();sb.AppendFormat("\t{0}\t{1}\t{2}\t{3}", OrderID, CustomerID, EmployeeID, OrderDate);sb.AppendLine();foreach (var item in OrderItems){sb.AppendFormat("\t\t{0}\t{1}\t{2}\n", item.Product.ProductName, item.UnitPrice, item.Quantity);}return sb.ToString();} } public class OrderItem {public int OrderId { get; set; }public Product Product { get; set; }public decimal UnitPrice { get; set; }public decimal Quantity { get; set; }}public class Product {public int ProductId { get; set; }public string ProductName { get; set; }}
}
第二部分:生成XSD的工具类(Utility.cs)
using System;
using System.Xml.Linq;
using System.Collections;
using System.Xml;namespace XMLDatabase
{public class Utility{/// <summary>/// 使用指定类型生成一个架构文件/// </summary>/// <typeparam name="T"></typeparam>public static void XsdGenerate<T>(XmlWriter xw) {Type t = typeof(T);XNamespace xn = "http://www.w3.org/2001/XMLSchema";XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),new XElement(xn + "schema",new XAttribute("elementFormDefault", "qualified"),new XAttribute(XNamespace.Xmlns + "xs", "http://www.w3.org/2001/XMLSchema"),new XElement(xn+"element",new XAttribute("name","Table"),new XAttribute("nillable","true"),new XAttribute("type","Table"))));XElement tableElement = new XElement(xn + "complexType",new XAttribute("name", "Table"));tableElement.Add(new XElement(xn + "sequence",new XElement(xn + "element",new XAttribute("minOccurs", "0"),new XAttribute("maxOccurs", "unbounded"),new XAttribute("name","Row"),new XAttribute("type",t.Name))),new XElement(xn + "attribute",new XAttribute("name", "CreateTime"),new XAttribute("type", "xs:string")));doc.Root.Add(tableElement);CreateComplexType(t, doc.Root);doc.Save(xw);}private static void CreateComplexType(Type t,XElement root) {XNamespace xn = root.GetNamespaceOfPrefix("xs");XElement temp = new XElement(xn + "complexType",new XAttribute("name", t.Name));#region 循环所有属性foreach (var p in t.GetProperties())//循环所有属性{Type pType = p.PropertyType;string fullType = pType.FullName;//这里仍然是分几种情况if (!GeneralType.Contains(fullType)){var seqelement = temp.Element(xn + "sequence");if (seqelement == null){seqelement = new XElement(xn + "sequence");temp.AddFirst(seqelement);}if (pType.IsEnum)//如果是枚举{seqelement.Add(new XElement(xn + "element",new XAttribute("minOccurs", "0"),new XAttribute("maxOccurs", "1"),new XAttribute("name", p.Name),new XAttribute("type", pType.Name)));XElement enumElement = new XElement(xn + "complexType",new XAttribute("name", pType.Name),new XElement(xn + "attribute",new XAttribute("name", "Enum"),new XAttribute("type", "xs:string")));root.Add(enumElement);}else if (pType.GetInterface(typeof(IList).FullName) != null && pType.IsGenericType)//如果是集合,并且是泛型集合{Type itemType = pType.GetGenericArguments()[0];seqelement.Add(new XElement(xn + "element",new XAttribute("minOccurs", "0"),new XAttribute("maxOccurs", "1"),new XAttribute("name", p.Name),new XAttribute("type", "ArrayOf"+p.Name)));XElement arrayElement = new XElement(xn + "complexType",new XAttribute("name", "ArrayOf" + p.Name),new XElement(xn + "sequence",new XElement(xn + "element",new XAttribute("minOccurs", "0"),new XAttribute("maxOccurs", "unbounded"),new XAttribute("name", itemType.Name),new XAttribute("type", itemType.Name))));root.Add(arrayElement);CreateComplexType(itemType, root);}else if (pType.IsClass || pType.IsValueType){seqelement.Add(new XElement(xn + "element",new XAttribute("minOccurs", "0"),new XAttribute("maxOccurs", "1"),new XAttribute("name", p.Name),new XAttribute("type", pType.Name)));CreateComplexType(pType, root);}}else{//这种情况最简单,属性为标准内置类型,直接作为元素的Attribute即可temp.Add(new XElement(xn + "attribute",new XAttribute("name", p.Name),new XAttribute("type", GeneralType.ConvertXSDType(pType.FullName))));}}#endregiontemp.Add(new XElement(xn + "attribute",new XAttribute("name", "TypeName"),new XAttribute("type", "xs:string")));root.Add(temp);}}
}
第三部分:辅助类型(GeneralType.cs).
这个类型中有一个方法可以将业务实体类型成员属性的类型转换为XSD中 的类型。
using System;
using System.Collections.Generic;
using System.Text;namespace XMLDatabase
{public class GeneralType{private static readonly List<string> generalTypes = new List<string>(){"System.Byte",//typeof(byte).FullName,"System.SByte",//typeof(sbyte).FullName,"System.Int16",//typeof(short).FullName,"System.UInt16",//typeof(ushort).FullName,"System.Int32",//typeof(int).FullName,"System.UInt32",//typeof(uint).FullName,"System.Int64",//typeof(long).FullName,"System.UInt64",//typeof(ulong).FullName,"System.Double",//typeof(double).FullName,"System.Decimal",//typeof(decimal).FullName,"System.Single",//typeof(float).FullName,"System.Char",//typeof(char).FullName,"System.Boolean",//typeof(bool).FullName,"System.String",//typeof(string).FullName,"System.DateTime"//typeof(DateTime).FullName};/// <summary>/// 判断当前给定类型是否为默认的数据类型/// </summary>/// <param name="fullType"></param>/// <returns></returns>public static bool Contains(string fullType){return generalTypes.Contains(fullType);}public static string ConvertXSDType(string fullType){switch (fullType){case "System.String":return "xs:string";case "System.Int32":return "xs:int";case "System.DateTime":return "xs:dateTime";case "System.Boolean":return "xs:boolean";case "System.Single":return "xs:float";case "System.Byte":return "xs:byte";case "System.SByte":return "xs:unsignedByte";case "System.Int16":return "xs:short";case "System.UInt16":return "xs:unsignedShort";case "System.UInt32":return "xs:unsignedInt";case "System.Int64":return "xs:long";case "System.UInt64":return "xs:unsignedLong";case "System.Double":return "xs:double";case "System.Decimal":return "xs:decimal";default:break;}return string.Empty;}}
}
第四部分:单元测试
/// <summary>///XsdGenerate 的测试///</summary>public void XsdGenerateTestHelper<T>(){XmlWriter xw = XmlWriter.Create("Order.xsd"); // TODO: 初始化为适当的值Utility.XsdGenerate<Order>(xw);xw.Close();}
第五部分: 生成的结果
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="Table" nillable="true" type="Table" /><xs:complexType name="Table"><xs:sequence><xs:element minOccurs="0" maxOccurs="unbounded" name="Row" type="Order" /></xs:sequence><xs:attribute name="CreateTime" type="xs:string" /></xs:complexType><xs:complexType name="ArrayOfOrderItems"><xs:sequence><xs:element minOccurs="0" maxOccurs="unbounded" name="OrderItem" type="OrderItem" /></xs:sequence></xs:complexType><xs:complexType name="Product"><xs:attribute name="ProductId" type="xs:int" /><xs:attribute name="ProductName" type="xs:string" /><xs:attribute name="TypeName" type="xs:string" /></xs:complexType><xs:complexType name="OrderItem"><xs:sequence><xs:element minOccurs="0" maxOccurs="1" name="Product" type="Product" /></xs:sequence><xs:attribute name="OrderId" type="xs:int" /><xs:attribute name="UnitPrice" type="xs:decimal" /><xs:attribute name="Quantity" type="xs:decimal" /><xs:attribute name="TypeName" type="xs:string" /></xs:complexType><xs:complexType name="Order"><xs:sequence><xs:element minOccurs="0" maxOccurs="1" name="OrderItems" type="ArrayOfOrderItems" /></xs:sequence><xs:attribute name="OrderID" type="xs:int" /><xs:attribute name="CustomerID" type="xs:string" /><xs:attribute name="EmployeeID" type="xs:int" /><xs:attribute name="OrderDate" type="xs:dateTime" /><xs:attribute name="TypeName" type="xs:string" /></xs:complexType>
</xs:schema>
第六部分:合法的数据文件范例
<?xml version="1.0" encoding="utf-8"?>
<Table Name="Orders" CreateTime="2009/8/9 21:59:04"><Row TypeName="DataEntities.Order" OrderID="10249" CustomerID="ABCDEF" EmployeeID="1" OrderDate="2009-08-09T21:59:04.125+08:00"><OrderItems><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="25" Quantity="4"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Pen" /></OrderItem><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="2" Quantity="2000"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Car" /></OrderItem></OrderItems></Row><Row TypeName="DataEntities.Order" OrderID="10249" CustomerID="ABCDEF" EmployeeID="1" OrderDate="2009-08-10T07:29:51.546875+08:00"><OrderItems><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="25" Quantity="4"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Pen" /></OrderItem><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="2" Quantity="2000"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Car" /></OrderItem></OrderItems></Row><Row TypeName="DataEntities.Order" OrderID="10249" CustomerID="ABCDEF" EmployeeID="1" OrderDate="2009-08-10T07:30:13.375+08:00"><OrderItems><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="25" Quantity="4"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Pen" /></OrderItem><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="2" Quantity="2000"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Car" /></OrderItem></OrderItems></Row><Row TypeName="DataEntities.Order" OrderID="10249" CustomerID="ABCDEF" EmployeeID="1" OrderDate="2009-08-10T07:30:43.875+08:00"><OrderItems><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="25" Quantity="4"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Pen" /></OrderItem><OrderItem TypeName="DataEntities.OrderItem" OrderId="10249" UnitPrice="2" Quantity="2000"><Product TypeName="DataEntities.Product" ProductId="1" ProductName="Car" /></OrderItem></OrderItems></Row>
</Table>
转载于:https://www.cnblogs.com/chenxizhang/archive/2009/08/10/1542746.html
.NET : 如何动态根据一个业务实体类型创建XSD架构文件相关推荐
- MSDN Visual系列:在MOSS中创建一个BDC实体
原文:http://msdn2.microsoft.com/en-us/library/bb410048.aspx 业务数据目录--BDC(Business Data Catalog)是MOSS中 ...
- 跨业务实体内部销售订单设定介绍
业务实体之间创建销售订单的设定 应该有两个业务实体.一个是目的地组织,另一个是来源组织. 1)选择职责: 人力资源 \人员 \输入和维护 输入姓氏,名字,人员类型. 点击分配: (职务和职位都 ...
- [答疑]运维记录台账本子识别为一个现状业务实体,妥否
软件方法(下)分析和设计第8章连载[20210816更新]分析 之 分析类图--知识篇 王周文(1034***424)19:24:54 请教潘老师:这个"现状业务序列图"中,我把这 ...
- java动态是如何根据实体建表_传入Java对象 自动创建动态表 并录入数据
看到Hibernate你给一个对象,他就能动态的创建配置文件里面指定的表名,然后把数据录入到数据库,当初感觉是很神奇,不过,好像 Hibernate不能动态的分表创建表和录入数据 我这里写了一个公用的 ...
- .NET业务实体类验证组件Fluent Validation
认识Fluent Vaidation. 看到NopCommerce项目中用到这个组建是如此的简单,将数据验证从业务实体类中分离出来,真是一个天才的想法,后来才知道这个东西是一个开源的轻量级验证组建. ...
- 推荐一个 Java 实体映射工具 MapStruct
声明: 1.DO(业务实体对象),DTO(数据传输对象). 2.我的代码中用到了 Lombok ,不了解的可以自行了解一下,了解的忽略这条就好. 在一个成熟的工程中,尤其是现在的分布式系统中,应用与应 ...
- 用C语言做一个横板过关类型的控制台游戏
前言:本教程是写给刚学会C语言基本语法不久的新生们. 因为在学习C语言途中,往往只能写控制台代码,而还能没接触到图形,也就基本碰不到游戏开发. 所以本教程希望可以给仍在学习C语言的新生们能提前感受到游 ...
- mysql mybatis 返回值类型_使用MyBatis时接收值和返回值选择Map类型或者实体类型
MyBatis作为现近JavaEE企业级项目开发中常用的持久层框架之一,以其简洁高效的ORM映射和高度的SQL的自由性被广大开发人员认可.Mybatis在接收系统传来的参数和返回的参数时主要可以有Ma ...
- Java 泛型获取实体类型
学习了动态初始化类,如果参数是各种类型的,要如何处理呢? 这时候,需要用到泛型,而传的数据是实体类型,如果从泛型中获取实体类型呢? 需要使用反射,获得指定类的父类的泛型参数的实际类型,直接上代码 ge ...
- 开源分布式Job系统,调度与业务分离-如何创建一个计划HttpJob任务
项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...
最新文章
- Android中的主题Theme
- Flex中你最应该知道的10点
- 数据结构 思维导图【绪论、线性表、栈、队列和数组、树与二叉树、图、查找、排序】
- restTemplate踩过的坑-spring clound--cloud内部服务调用重试次数
- Error creating bean with name ‘cn.cyjt.shoot.service.UserServiceTests‘: Unsatisfied dependency expre
- 安装教程rtx2080ti_点云深度学习环境配置指南(一)Ubuntu16.04+RTX2080ti
- javascript Event监听
- infor wms 中英文对照_WMS系统中波次管理有多大作用
- 清除每隔5000毫秒请求一次接口的定时器(需求:每当我手动核销电子码,页面上的显示数据要实时更新到)...
- mini_magick
- csdn广告屏蔽方法
- delphi uniDac
- FreeRTOS-CortexM4-相关函数说明
- 准备走上共享软件之路,出师不利,两块石头石沉大海,我打算流了她们。
- 基于 STM32对音频数据的 Flash 读取与 DAC 播放
- 【2020年高被引学者】 陶大程 悉尼大学
- HCIE 面试资料-IPv6
- 指针式仪表自动读数与识别(四):非圆形表盘定位
- 【武器系统】【2011.12】巡航导弹任务演习
- SEO优化人员必备的几种关键词优化工具_零距离seo