溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么動態(tài)根據(jù)一個(gè)業(yè)務(wù)實(shí)體類型創(chuàng)建XSD架構(gòu)文件

發(fā)布時(shí)間:2021-12-16 16:27:24 來源:億速云 閱讀:234 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“怎么動態(tài)根據(jù)一個(gè)業(yè)務(wù)實(shí)體類型創(chuàng)建XSD架構(gòu)文件”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“怎么動態(tài)根據(jù)一個(gè)業(yè)務(wù)實(shí)體類型創(chuàng)建XSD架構(gòu)文件”這篇文章吧。

目前該功能僅僅是一個(gè)原型,還有待細(xì)化改進(jìn)。例如在實(shí)體的成員上用一些特定的Attribute標(biāo)明該成員要被保存的形態(tài)。

創(chuàng)建XSD架構(gòu)文件***部分:業(yè)務(wù)實(shí)體類

(作為演示目的,我將所有的類型定義在一個(gè)文件里,同時(shí)每個(gè)類都只有少量簡單的屬性成員)

此處特別注意的是,Order這個(gè)類是很復(fù)雜的,它包含了一系列的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; }       }  }

創(chuàng)建XSD架構(gòu)文件第二部分:生成XSD的工具類(Utility.cs)

using System;  using System.Xml.Linq;  using System.Collections;  using System.Xml;   namespace XMLDatabase  {      public class Utility      {          /// <summary>         /// 使用指定類型生成一個(gè)架構(gòu)文件          /// </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 循環(huán)所有屬性               foreach (var p in t.GetProperties())//循環(huán)所有屬性              {                  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                  {                      //這種情況最簡單,屬性為標(biāo)準(zhǔn)內(nèi)置類型,直接作為元素的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);          }       }  }

創(chuàng)建XSD架構(gòu)文件第三部分:輔助類型(GeneralType.cs).

這個(gè)類型中有一個(gè)方法可以將業(yè)務(wù)實(shí)體類型成員屬性的類型轉(zhuǎn)換為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>         /// 判斷當(dāng)前給定類型是否為默認(rèn)的數(shù)據(jù)類型          /// </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;          }                }  }

創(chuàng)建XSD架構(gòu)文件第四部分:單元測試

/// <summary> ///XsdGenerate 的測試  ///</summary> public void XsdGenerateTestHelper<T>()  {      XmlWriter xw = XmlWriter.Create("Order.xsd"); // TODO: 初始化為適當(dāng)?shù)闹?nbsp;     Utility.XsdGenerate<Order>(xw);       xw.Close();  }

創(chuàng)建XSD架構(gòu)文件第五部分: 生成的結(jié)果

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

創(chuàng)建XSD架構(gòu)文件第六部分:合法的數(shù)據(jù)文件范例

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

以上是“怎么動態(tài)根據(jù)一個(gè)業(yè)務(wù)實(shí)體類型創(chuàng)建XSD架構(gòu)文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

xsd
AI