iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么动态根据一个业务实体类型创建XSD架构文件
  • 451
分享到

怎么动态根据一个业务实体类型创建XSD架构文件

2023-06-18 00:06:21 451人浏览 薄情痞子
摘要

这篇文章主要为大家展示了“怎么动态根据一个业务实体类型创建XSD架构文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么动态根据一个业务实体类型创建XSD架构文件”这篇文章吧。目前该功能仅仅是

这篇文章主要为大家展示了“怎么动态根据一个业务实体类型创建XSD架构文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么动态根据一个业务实体类型创建XSD架构文件”这篇文章吧。

目前该功能仅仅是一个原型,还有待细化改进。例如在实体的成员上用一些特定的Attribute标明该成员要被保存的形态。

创建XSD架构文件***部分:业务实体类

(作为演示目的,我将所有的类型定义在一个文件里,同时每个类都只有少量简单的属性成员)

此处特别注意的是,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架构文件第二部分:生成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 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>         /// <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;          }                }  }

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

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

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

<?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>

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

<?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>

以上是“怎么动态根据一个业务实体类型创建XSD架构文件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: 怎么动态根据一个业务实体类型创建XSD架构文件

本文链接: https://www.lsjlt.com/news/294875.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么动态根据一个业务实体类型创建XSD架构文件
    这篇文章主要为大家展示了“怎么动态根据一个业务实体类型创建XSD架构文件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么动态根据一个业务实体类型创建XSD架构文件”这篇文章吧。目前该功能仅仅是...
    99+
    2023-06-18
  • SpringBoot+jpa配置怎么根据实体类自动创建表
    这篇文章主要介绍“SpringBoot+jpa配置怎么根据实体类自动创建表”,在日常操作中,相信很多人在SpringBoot+jpa配置怎么根据实体类自动创建表问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”S...
    99+
    2023-06-25
  • Linux中怎么创建一个FIFO文件类型
    这篇文章将为大家详细讲解有关Linux中怎么创建一个FIFO文件类型,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。  FIFO,又称作命名管道(named pipe),它是Linux系统中用...
    99+
    2023-06-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作