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

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. [转] Matlab编程规范(MATLAB Programming Style Guidelines)

    转自: Jerry Zitao Liu的博客 主要是参考了下面这篇文章,简洁总结在这里. MATLAB Programming Style Guidelines 简洁总结如下: 表示object的数量 ...

  2. HexDump.java解析,android 16进制转换

    HexDump.java解析android 16进制转换 package com.android.internal.util; public class HexDump { private final ...

  3. zookeeper与卡夫卡集群搭建

    首先这片博客没有任何理论性的东西,只是详细说明kafka与zookeeper集群的搭建过程,需要三台linux服务器. java环境变量设置 zookeeper集群搭建 kafka集群搭建 java环 ...

  4. bzoj1635 / P2879 [USACO07JAN]区间统计Tallest Cow

    P2879 [USACO07JAN]区间统计Tallest Cow 差分 对于每个限制$(l,r)$,我们建立一个差分数组$a[i]$ 使$a[l+1]--,a[r]++$,表示$(l,r)$区间内的 ...

  5. P1771 方程的解_NOI导刊2010提高(01)

    P1771 方程的解_NOI导刊2010提高(01) 按题意用快速幂把$g(x)$求出来 发现这不就是个组合数入门题吗! $k$个人分$g(x)$个苹果,每人最少分$1$个,有几种方法? 根据插板法, ...

  6. 简单的Django实现图片上传,并存储进MySQL数据库 案例——小白

    目标:通过网页上传一张图片到Django后台,后台接收并存储进数据库 真是不容易!!这个案例的代码网上太乱,不适合我,自己摸索着写,终于成功了,记录一下,仅供自己参考,有的解释可能不对,自己明白就好, ...

  7. 更改 Centos 6 的 yum 源

    1.查看当前使用的源: yum repolist all 阿里源网址,使用方法点右边的帮助可以看到:https://opsx.alibaba.com/mirror 2.更改源: 第一步:备份你的原镜像 ...

  8. 【前端】Vue.js实现网格列表布局转换

    网格列表布局转换 实现效果: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>布局转换</title& ...

  9. linux下安装与运行docker

    写者环境: 1.lsb_release -a hello@hello:~$ lsb_release -aNo LSB modules are available.Distributor ID: Ubu ...

  10. zedgraph右键菜单的汉化

    http://blog.csdn.net/jeryler/article/details/7876376 修改 zedGraphControl的ContextMenuBuilder事件即可! zedG ...