------------------------------网上参考资料

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace DataEntities
  5. {
  6. public class Order
  7. {
  8. public int OrderID { get; set; }
  9. public string CustomerID { get; set; }
  10. public int EmployeeID { get; set; }
  11. public DateTime OrderDate { get; set; }
  12. public List<OrderItem> OrderItems { get; set; }
  13. public override string ToString()
  14. {
  15. StringBuilder sb = new StringBuilder();
  16. sb.AppendFormat("\t{0}\t{1}\t{2}\t{3}", OrderID, CustomerID, EmployeeID, OrderDate);
  17. sb.AppendLine();
  18. foreach (var item in OrderItems)
  19. {
  20. sb.AppendFormat("\t\t{0}\t{1}\t{2}\n", item.Product.ProductName, item.UnitPrice, item.Quantity);
  21. }
  22. return sb.ToString();
  23. }
  24. }
  25. public class OrderItem
  26. {
  27. public int OrderId { get; set; }
  28. public Product Product { get; set; }
  29. public decimal UnitPrice { get; set; }
  30. public decimal Quantity { get; set; }
  31. }
  32. public class Product
  33. {
  34. public int ProductId { get; set; }
  35. public string ProductName { get; set; }
  36. }
  37. }

创建XSD架构文件第二部分:生成XSD的工具类(Utility.cs)

  1. using System;
  2. using System.Xml.Linq;
  3. using System.Collections;
  4. using System.Xml;
  5. namespace XMLDatabase
  6. {
  7. public class Utility
  8. {
  9. /// <summary>
  10. /// 使用指定类型生成一个架构文件
  11. /// </summary>
  12. /// <typeparamname="T"></typeparam>
  13. public static void XsdGenerate<T>(XmlWriter xw) {
  14. Type t = typeof(T);
  15. XNamespace xn = "http://www.w3.org/2001/XMLSchema";
  16. XDocument doc = new XDocument(
  17. new XDeclaration("1.0", "utf-8", "yes"),
  18. new XElement(xn + "schema",
  19. new XAttribute("elementFormDefault", "qualified"),
  20. new XAttribute(XNamespace.Xmlns + "xs", "http://www.w3.org/2001/XMLSchema"),
  21. new XElement(xn+"element",
  22. new XAttribute("name","Table"),
  23. new XAttribute("nillable","true"),
  24. new XAttribute("type","Table"))
  25. ));
  26. XElement tableElement = new XElement(xn + "complexType",
  27. new XAttribute("name", "Table"));
  28. tableElement.Add(
  29. new XElement(xn + "sequence",
  30. new XElement(xn + "element",
  31. new XAttribute("minOccurs", "0"),
  32. new XAttribute("maxOccurs", "unbounded"),
  33. new XAttribute("name","Row"),
  34. new XAttribute("type",t.Name)
  35. )),
  36. new XElement(xn + "attribute",
  37. new XAttribute("name", "CreateTime"),
  38. new XAttribute("type", "xs:string"))
  39. );
  40. doc.Root.Add(tableElement);
  41. CreateComplexType(t, doc.Root);
  42. doc.Save(xw);
  43. }
  44. private static void CreateComplexType(Type t,XElement root) {
  45. XNamespace xn = root.GetNamespaceOfPrefix("xs");
  46. XElement temp = new XElement(
  47. xn + "complexType",
  48. new XAttribute("name", t.Name));
  49. #region 循环所有属性
  50. foreach (var p in t.GetProperties())//循环所有属性
  51. {
  52. Type ppType = p.PropertyType;
  53. string fullType = pType.FullName;
  54. //这里仍然是分几种情况
  55. if (!GeneralType.Contains(fullType))
  56. {
  57. var seqelement = temp.Element(xn + "sequence");
  58. if (seqelement == null)
  59. {
  60. seqelement = new XElement(xn + "sequence");
  61. temp.AddFirst(seqelement);
  62. }
  63. if (pType.IsEnum)//如果是枚举
  64. {
  65. seqelement.Add(
  66. new XElement(
  67. xn + "element",
  68. new XAttribute("minOccurs", "0"),
  69. new XAttribute("maxOccurs", "1"),
  70. new XAttribute("name", p.Name),
  71. new XAttribute("type", pType.Name)));
  72. XElement enumElement = new XElement(
  73. xn + "complexType",
  74. new XAttribute("name", pType.Name),
  75. new XElement(xn + "attribute",
  76. new XAttribute("name", "Enum"),
  77. new XAttribute("type", "xs:string")));
  78. root.Add(enumElement);
  79. }
  80. else if (pType.GetInterface(typeof(IList).FullName) != null && pType.IsGenericType)
  81. //如果是集合,并且是泛型集合
  82. {
  83. Type itemType = pType.GetGenericArguments()[0];
  84. seqelement.Add(
  85. new XElement(
  86. xn + "element",
  87. new XAttribute("minOccurs", "0"),
  88. new XAttribute("maxOccurs", "1"),
  89. new XAttribute("name", p.Name),
  90. new XAttribute("type", "ArrayOf"+p.Name)));
  91. XElement arrayElement = new XElement(
  92. xn + "complexType",
  93. new XAttribute("name", "ArrayOf" + p.Name),
  94. new XElement(xn + "sequence",
  95. new XElement(xn + "element",
  96. new XAttribute("minOccurs", "0"),
  97. new XAttribute("maxOccurs", "unbounded"),
  98. new XAttribute("name", itemType.Name),
  99. new XAttribute("type", itemType.Name))));
  100. root.Add(arrayElement);
  101. CreateComplexType(itemType, root);
  102. }
  103. else if (pType.IsClass || pType.IsValueType)
  104. {
  105. seqelement.Add(
  106. new XElement(
  107. xn + "element",
  108. new XAttribute("minOccurs", "0"),
  109. new XAttribute("maxOccurs", "1"),
  110. new XAttribute("name", p.Name),
  111. new XAttribute("type", pType.Name)));
  112. CreateComplexType(pType, root);
  113. }
  114. }
  115. else
  116. {
  117. //这种情况最简单,属性为标准内置类型,直接作为元素的Attribute即可
  118. temp.Add(
  119. new XElement(xn + "attribute",
  120. new XAttribute("name", p.Name),
  121. new XAttribute("type", GeneralType.ConvertXSDType(pType.FullName))));
  122. }
  123. }
  124. #endregion
  125. temp.Add(new XElement(xn + "attribute",
  126. new XAttribute("name", "TypeName"),
  127. new XAttribute("type", "xs:string")));
  128. root.Add(temp);
  129. }
  130. }
  131. }

创建XSD架构文件第三部分:辅助类型(GeneralType.cs).

这个类型中有一个方法可以将业务实体类型成员属性的类型转换为XSD中 的类型。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace XMLDatabase
  5. {
  6. public class GeneralType
  7. {
  8. private static readonly List<string>generalTypes = new List<string>()
  9. {
  10. "System.Byte",//typeof(byte).FullName,
  11. "System.SByte",//typeof(sbyte).FullName,
  12. "System.Int16",//typeof(short).FullName,
  13. "System.UInt16",//typeof(ushort).FullName,
  14. "System.Int32",//typeof(int).FullName,
  15. "System.UInt32",//typeof(uint).FullName,
  16. "System.Int64",//typeof(long).FullName,
  17. "System.UInt64",//typeof(ulong).FullName,
  18. "System.Double",//typeof(double).FullName,
  19. "System.Decimal",//typeof(decimal).FullName,
  20. "System.Single",//typeof(float).FullName,
  21. "System.Char",//typeof(char).FullName,
  22. "System.Boolean",//typeof(bool).FullName,
  23. "System.String",//typeof(string).FullName,
  24. "System.DateTime"//typeof(DateTime).FullName
  25. };
  26. /// <summary>
  27. /// 判断当前给定类型是否为默认的数据类型
  28. /// </summary>
  29. /// <paramname="fullType"></param>
  30. /// <returns></returns>
  31. public static bool Contains(string fullType)
  32. {
  33. return generalTypes.Contains(fullType);
  34. }
  35. public static string ConvertXSDType(string fullType)
  36. {
  37. switch (fullType)
  38. {
  39. case "System.String":
  40. return "xs:string";
  41. case "System.Int32":
  42. return "xs:int";
  43. case "System.DateTime":
  44. return "xs:dateTime";
  45. case "System.Boolean":
  46. return "xs:boolean";
  47. case "System.Single":
  48. return "xs:float";
  49. case "System.Byte":
  50. return "xs:byte";
  51. case "System.SByte":
  52. return "xs:unsignedByte";
  53. case "System.Int16":
  54. return "xs:short";
  55. case "System.UInt16":
  56. return "xs:unsignedShort";
  57. case "System.UInt32":
  58. return "xs:unsignedInt";
  59. case "System.Int64":
  60. return "xs:long";
  61. case "System.UInt64":
  62. return "xs:unsignedLong";
  63. case "System.Double":
  64. return "xs:double";
  65. case "System.Decimal":
  66. return "xs:decimal";
  67. default:
  68. break;
  69. }
  70. return string.Empty;
  71. }
  72. }
  73. }

创建XSD架构文件第四部分:单元测试

  1. /// <summary>
  2. ///XsdGenerate 的测试
  3. ///</summary>
  4. public void XsdGenerateTestHelper<T>()
  5. {
  6. XmlWriter xw = XmlWriter.Create("Order.xsd"); // TODO: 初始化为适当的值
  7. Utility.XsdGenerate<Order>(xw);
  8. xw.Close();
  9. }

创建XSD架构文件第五部分: 生成的结果

  1. <?xmlversion="1.0"encoding="utf-8"standalone="yes"?>
  2. <xs:schemaelementFormDefault="qualified"xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3. <xs:elementname="Table"nillable="true"type="Table"/>
  4. <xs:complexTypename="Table">
  5. <xs:sequence>
  6. <xs:elementminOccurs="0"maxOccurs="unbounded"name="Row"type="Order"/>
  7. </xs:sequence>
  8. <xs:attributename="CreateTime"type="xs:string"/>
  9. </xs:complexType>
  10. <xs:complexTypename="ArrayOfOrderItems">
  11. <xs:sequence>
  12. <xs:elementminOccurs="0"maxOccurs="unbounded"name="OrderItem"type="OrderItem"/>
  13. </xs:sequence>
  14. </xs:complexType>
  15. <xs:complexTypename="Product">
  16. <xs:attributename="ProductId"type="xs:int"/>
  17. <xs:attributename="ProductName"type="xs:string"/>
  18. <xs:attributename="TypeName"type="xs:string"/>
  19. </xs:complexType>
  20. <xs:complexTypename="OrderItem">
  21. <xs:sequence>
  22. <xs:elementminOccurs="0"maxOccurs="1"name="Product"type="Product"/>
  23. </xs:sequence>
  24. <xs:attributename="OrderId"type="xs:int"/>
  25. <xs:attributename="UnitPrice"type="xs:decimal"/>
  26. <xs:attributename="Quantity"type="xs:decimal"/>
  27. <xs:attributename="TypeName"type="xs:string"/>
  28. </xs:complexType>
  29. <xs:complexTypename="Order">
  30. <xs:sequence>
  31. <xs:elementminOccurs="0"maxOccurs="1"name="OrderItems"type="ArrayOfOrderItems"/>
  32. </xs:sequence>
  33. <xs:attributename="OrderID"type="xs:int"/>
  34. <xs:attributename="CustomerID"type="xs:string"/>
  35. <xs:attributename="EmployeeID"type="xs:int"/>
  36. <xs:attributename="OrderDate"type="xs:dateTime"/>
  37. <xs:attributename="TypeName"type="xs:string"/>
  38. </xs:complexType>
  39. </xs:schema>

创建XSD架构文件第六部分:合法的数据文件范例

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TableName="Orders"CreateTime="2009/8/9 21:59:04">
  3. <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-09T21:59:04.125+08:00">
  4. <OrderItems>
  5. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
  6. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
  7. </OrderItem>
  8. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
  9. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
  10. </OrderItem>
  11. </OrderItems>
  12. </Row>
  13. <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:29:51.546875+08:00">
  14. <OrderItems>
  15. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
  16. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
  17. </OrderItem>
  18. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
  19. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
  20. </OrderItem>
  21. </OrderItems>
  22. </Row>
  23. <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:30:13.375+08:00">
  24. <OrderItems>
  25. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
  26. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
  27. </OrderItem>
  28. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
  29. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
  30. </OrderItem>
  31. </OrderItems>
  32. </Row>
  33. <RowTypeName="DataEntities.Order"OrderID="10249"CustomerID="ABCDEF"EmployeeID="1"OrderDate="2009-08-10T07:30:43.875+08:00">
  34. <OrderItems>
  35. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="25"Quantity="4">
  36. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Pen"/>
  37. </OrderItem>
  38. <OrderItemTypeName="DataEntities.OrderItem"OrderId="10249"UnitPrice="2"Quantity="2000">
  39. <ProductTypeName="DataEntities.Product"ProductId="1"ProductName="Car"/>
  40. </OrderItem>
  41. </OrderItems>
  42. </Row>
  43. </Table>

XSD与C#Code以及XML之间的相互关心的更多相关文章

  1. JAVA Bean和XML之间的相互转换 - XStream简单入门

    JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...

  2. 利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD

    利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD 1, 命令提示符-->找到vs自带的xsd.exe工具所在的文件夹 例如: C:\Program Files ...

  3. 使用JAXB来实现Java合xml之间的转换

    使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContex ...

  4. WebService(2)-XML系列之Java和Xml之间相互转换

    源代码下载:链接:http://pan.baidu.com/s/1ntL1a7R password: rwp1 本文主要讲述:使用jaxb完毕对象和xml之间的转换 TestJava2xml.java ...

  5. java与xml之间的转换(jaxb)

    使用java提供的JAXB来实现java到xml之间的转换,先创建两个持久化的类(Student和Classroom): Classroom: package com.model; public cl ...

  6. JAXB实现java对象与xml之间转换

    JAXB简介: 1.JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标 ...

  7. Xml与DataTable相互转换方法

    1.Xml与DataTable相互转换方法:http://www.cnblogs.com/lilin/archive/2010/04/18/1714927.html

  8. 别名现象,java对象之间的相互赋值

    请看一下代码 import java.util.*; class book{ static  int c = null; } public static void main(String[] args ...

  9. JAVA和C/C++之间的相互调用。

    在一些Android应用的开发中,需要通过JNI和 Android NDK工具实现JAVA和C/C++之间的相互调用. Java Native Interface (JNI)标准是java平台的一部分 ...

随机推荐

  1. webstorm的个性化设置settings

    如何更改主题(字体&配色):File -> settings -> Editor -> colors&fonts -> scheme name.主题下载地址 如 ...

  2. java多线程-----volatile

    谈谈Java中的volatile   内存可见性 留意复合类操作 解决num++操作的原子性问题 禁止指令重排序 总结 内存可见性 volatile是Java提供的一种轻量级的同步机制,在并发编程中, ...

  3. Java笔记 #02# 带资源的try语句

    索引 普通的 try.java 带资源的 try.java 当资源为 null 的情况 可以参考的文档与资料 / test.txt 待读取的内容 hello. / 普通的 try.java 读取 te ...

  4. bzoj1651 / P2859 [USACO06FEB]摊位预订Stall Reservations

    P2859 [USACO06FEB]摊位预订Stall Reservations 维护一个按右端点从小到大的优先队列 蓝后把数据按左端点从小到大排序,顺序枚举. 每次把比右端点比枚举线段左端点小的数据 ...

  5. Python3基础 os listdir curdir 查看当前工作目录的所有文件的名字

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. Go第三篇之大话容器

    Go语言数组 数组(Array)是一段固定长度的连续内存区域 在 Go 语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化 Go 语言数组的声明 数组的写法如下: var 数组变 ...

  7. 不同ContentType的post请求

    public static T Invoke<T>(string url, object input, bool requireJSON = true) { using (var clie ...

  8. SPA (单页应用程序)

    单页Web应用 编辑 单页Web应用(single page web application,SPA),就是只有一张Web页面的应用.单页应用程序 (SPA) 是加载单个HTML 页面并在用户与应用程 ...

  9. [JVM] - 继10进制的java.lang.Object查看之后

    cmd清除命令:cls 之后查阅了其它博客,发现这位大神同样也在做JVM,并且我很希望用它的10进制转16进制类来测试一下该解析的10进制是否对应着Object的16进制呢? 这位大神的10进制转16 ...

  10. 命令模式(head first 设计模式5)

    一.命令模式定义 命令大家都不会陌生,那么在开始命令模式之前,可以想象一下生活中的命令模式的特点: 如老板命令你完成一个OA项目是一个命令,接着看看其特点: 1.在上面的命令中,命令的执行者肯定是聪明 ...