XSD与C#Code以及XML之间的相互关心

时间:2023-11-24 23:06:50

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

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>