XSD与C#Code以及XML之间的相互关心
------------------------------网上参考资料
C# 利用自带xsd.exe工具操作XML-如通过XML生成xsd文件:http://blog.sina.com.cn/s/blog_7a8de3410100xlyl.html
xsd文件转换为实体类:http://code.3rbang.com/xsdtoclass/
如何动态根据一个业务实体类型创建XSD架构文件:http://developer.51cto.com/art/200908/143058.htm
- 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架构文件第二部分:生成XSD的工具类(Utility.cs)
- using System;
- using System.Xml.Linq;
- using System.Collections;
- using System.Xml;
- namespace XMLDatabase
- {
- public class Utility
- {
- /// <summary>
- /// 使用指定类型生成一个架构文件
- /// </summary>
- /// <typeparamname="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 ppType = 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))));
- }
- }
- #endregion
- temp.Add(new XElement(xn + "attribute",
- new XAttribute("name", "TypeName"),
- new XAttribute("type", "xs:string")));
- root.Add(temp);
- }
- }
- }
创建XSD架构文件第三部分:辅助类型(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>
- /// <paramname="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;
- }
- }
- }
创建XSD架构文件第四部分:单元测试
- /// <summary>
- ///XsdGenerate 的测试
- ///</summary>
- public void XsdGenerateTestHelper<T>()
- {
- XmlWriter xw = XmlWriter.Create("Order.xsd"); // TODO: 初始化为适当的值
- Utility.XsdGenerate<Order>(xw);
- xw.Close();
- }
创建XSD架构文件第五部分: 生成的结果
- <?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
- <xs:schemaelementFormDefault="qualified"xmlns:xs="http://www.w3.org/2001/XMLSchema">
- <xs:elementname="Table"nillable="true"type="Table"/>
- <xs:complexTypename="Table">
- <xs:sequence>
- <xs:elementminOccurs="0"maxOccurs="unbounded"name="Row"type="Order"/>
- </xs:sequence>
- <xs:attributename="CreateTime"type="xs:string"/>
- </xs:complexType>
- <xs:complexTypename="ArrayOfOrderItems">
- <xs:sequence>
- <xs:elementminOccurs="0"maxOccurs="unbounded"name="OrderItem"type="OrderItem"/>
- </xs:sequence>
- </xs:complexType>
- <xs:complexTypename="Product">
- <xs:attributename="ProductId"type="xs:int"/>
- <xs:attributename="ProductName"type="xs:string"/>
- <xs:attributename="TypeName"type="xs:string"/>
- </xs:complexType>
- <xs:complexTypename="OrderItem">
- <xs:sequence>
- <xs:elementminOccurs="0"maxOccurs="1"name="Product"type="Product"/>
- </xs:sequence>
- <xs:attributename="OrderId"type="xs:int"/>
- <xs:attributename="UnitPrice"type="xs:decimal"/>
- <xs:attributename="Quantity"type="xs:decimal"/>
- <xs:attributename="TypeName"type="xs:string"/>
- </xs:complexType>
- <xs:complexTypename="Order">
- <xs:sequence>
- <xs:elementminOccurs="0"maxOccurs="1"name="OrderItems"type="ArrayOfOrderItems"/>
- </xs:sequence>
- <xs:attributename="OrderID"type="xs:int"/>
- <xs:attributename="CustomerID"type="xs:string"/>
- <xs:attributename="EmployeeID"type="xs:int"/>
- <xs:attributename="OrderDate"type="xs:dateTime"/>
- <xs:attributename="TypeName"type="xs:string"/>
- </xs:complexType>
- </xs:schema>
创建XSD架构文件第六部分:合法的数据文件范例
- <?xmlversion="1.0"encoding="utf-8"?>
- <TableName="Orders"CreateTime="2009/8/9 21:59:04">
- <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-09T21:59:04.125+08:00">
- <OrderItems>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
- </OrderItem>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
- </OrderItem>
- </OrderItems>
- </Row>
- <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:29:51.546875+08:00">
- <OrderItems>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
- </OrderItem>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
- </OrderItem>
- </OrderItems>
- </Row>
- <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:30:13.375+08:00">
- <OrderItems>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
- </OrderItem>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
- </OrderItem>
- </OrderItems>
- </Row>
- <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:30:43.875+08:00">
- <OrderItems>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
- </OrderItem>
- <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
- <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
- </OrderItem>
- </OrderItems>
- </Row>
- </Table>
XSD与C#Code以及XML之间的相互关心的更多相关文章
- JAVA Bean和XML之间的相互转换 - XStream简单入门
JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...
- 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD
利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD 1, 命令提示符-->找到vs自带的xsd.exe工具所在的文件夹 例如: C:\Program Files ...
- 使用JAXB来实现Java合xml之间的转换
使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContex ...
- WebService(2)-XML系列之Java和Xml之间相互转换
源代码下载:链接:http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:使用jaxb完毕对象和xml之间的转换 TestJava2xml.java ...
- java与xml之间的转换(jaxb)
使用java提供的JAXB来实现java到xml之间的转换,先创建两个持久化的类(Student和Classroom): Classroom: package com.model; public cl ...
- JAXB实现java对象与xml之间转换
JAXB简介: 1.JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标 ...
- Xml与DataTable相互转换方法
1.Xml与DataTable相互转换方法:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html
- 别名现象,java对象之间的相互赋值
请看一下代码 import java.util.*; class book{ static int c = null; } public static void main(String[] args ...
- JAVA和C/C++之间的相互调用。
在一些Android应用的开发中,需要通过JNI和 Android NDK工具实现JAVA和C/C++之间的相互调用. Java Native Interface (JNI)标准是java平台的一部分 ...
随机推荐
- Trove系列(八)——Trove的配置管理相关的功能介绍
概述MySQL 配置管理功能允许Trove 用户重载由Trove服务的操作者提供的缺省MySQL配置环境.这是通过影响MySQL 的includedir 命令来实现的.这些MySQL 的include ...
- ac1008
这题说的是 给了n个点在圆 上 然后 i连 i+2 从i+2 开始连 i+4 然后 这样一直到某个点 已经被连过为止 如果还有的没有被连过 就从那个点开始 连 按照上面的规则 当 N大于6 的时 ...
- Linux中顿号
``的作用是运行``之间的命令,并且将命令运行的结果返回.一般shell脚本应该是这样:result=`ls -l` (用你的命令替换ls -l,这里只是举例)这样,result就有``里面的运行结果 ...
- Java应用开发的一条重要经验:先建立基础设施
一旦为应用建立良好的基础设施, 后续的开发就会变得容易而快速.这些基础设施包括: 1. 线程池的建立与配置: 在 JDK 并发库的基础上建立适合于应用的多任务接口和框架: 2. 外部系统服务 ...
- JVisualVM远程监控
本文主要转自:http://blog.iamzsx.me/show.html?id=394002 http://www.wl566.com/wz/64022.html 1. 通过JMX方式监控远程li ...
- OpenGL边用边学------2 经典照相机模型
https://blog.csdn.net/smstong/article/details/50290327 实际照相步骤 1 布置场景和调整照相机位置 3 选择镜头对焦Focus 4 按下快门 5 ...
- yum配合rpm查看软件包安装位置
今天安装apache,新版本要求除了apache的安装包以外,还要求先安装apr.apr-util和pcre. 开始并没有急着去下载apr的安装包,而是想看看我的操作系统中有没有安装过了这个软件,结果 ...
- 基于Swoole和Redis实现的并发队列处理系统
背景 由于PHP不支持多线程,但是作为一个完善的系统,有很多操作都是需要异步完成的.为了完成这些异步操作,我们做了一个基于Redis队列任务系统. 大家知道,一个消息队列处理系统主要分为两大部分:消费 ...
- 09: python基础补充
1.1 闭包 1.闭包概念 1. 在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用,这样就构成了一个闭包 2. 一般情况下,在我们认知当中,如果一个函数 ...
- Android项目开发四
微博客户端开发 本周学习计划 研究微博客户端关于Sqlite数据库代码. 完成微博撰写.发布等功能模块. 将程序中存在的问题解决. 实际完成情况 Sqlite数据库学习与研究 微博客户端功能设定中涉及 ...