C # XML and XML Schema documents generated by the entity class,
I. entity class generation XML
1 private void CreateXML () 2 {3 Type [] objType = DBEntityRegst (); 4 foreach (var item in objType) 5 {6 if (item = null) 7 {8 break; 9} 10 11 XmlDocument doc = new XmlDocument (); 12 13 // <? Xml version = "1.0"?> 14 XmlDeclaration dec = doc. createXmlDeclaration ("1.0", "", ""); 15 doc. appendChild (dec); 16 17 // create a root node 18 // <schema xmlns: xs =" http://www.w3.org/2001/XMLSchema "> 19 XmlElement root = doc. CreateElement (" schema "); 20 root. SetAttribute (" xmlns: xs "," http://www.w3.org/2001/XMLSchema "); 21 doc. appendChild (root); 22 23 // <element Name = "DTO_BatchTotal"> 24 XmlElement element1 = doc. createElement ("element"); 25 element1.SetAttribute ("Name", (item. name ). toString (); 26 27 // <complexType> 28 XmlElement element2 = doc. createElement ("complexType"); 29 30 // <sequence> 31 XmlElement element3 = doc. createElement ("sequence"); 32 33 34 System. reflection. propertyInfo [] ps = item. getProperties (); 35 for (int I = 0; I <ps. length; I ++) 36 {37 string typeold = (ps [I]. propertyType ). toString (); 38 string [] a = typeold. split ('. '); 39 string typenew = ("xs:" + a [1]). toLower (); 40 41 // <element Name = "batch" Type = "xs: string"/> 42 // <element Name = "total" Type = "xs: decimal "/> 43 XmlElement element4 = doc. createElement ("element"); 44 element4.SetAttribute ("Name", ps [I]. name); 45 element4.SetAttribute ("Type", typenew); 46 47 // Add a subnode 48 element3.AppendChild (element4) to element3; 49} 50 // same as 51 element1.AppendChild (element2 ); 52 element2.AppendChild (element3); 53 root. appendChild (element1); 54 55 XmlSerializerNamespaces xsn = new XmlSerializerNamespaces (); 56 xsn. add ("xs "," http://www.w3.org/2001/XMLSchema "); 57 doc. save (@ "E: \ XML \" + item. name. toString () + ". xml "); 58 59 60} 61} 62 63 // <summary> 64 // load the DTO object to List 65 /// </summary> 66 private Type [] DBEntityRegst () 67 {68 69 List <IEntityRegstHelper> entityRegstHelperlist = null; 70 entityRegstHelperlist = new List <IEntityRegstHelper> (); 71 var classes = Assembly. load ("ZxSoftERPGC. model "). getTypes (); // The class Type set 72 73 Type [] a = new Type [classes. length]; 74 int I = 0; 75 foreach (var classtype in classes) 76 {77 78 if (classtype. name ). toString ()). contains ("DTO _") // specify to get the class name containing "DTO _" 79 {80 a [I] = classtype; 81 I ++; 82} 83} 84 return a; 85}
The result is as follows:
<?xml version="1.0"?><schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <element Name="DTO_BatchTotal"> <complexType> <sequence> <element Name="batch" Type="xs:string" /> <element Name="total" Type="xs:decimal" /> </sequence> </complexType> </element></schema>
Reference: http://www.cnblogs.com/xwdreamer/archive/2011/04/27/2297023.html
Ii. Generate XML Schema
Private void button#click (object sender, EventArgs e) {Type [] objType = DBEntityRegst (); foreach (var item in objType) {if (item = null) {break ;} xmlSchema schema = new XmlSchema (); // <xs: element name = ""> XmlSchemaElement elementPets = new XmlSchemaElement (); schema. items. add (elementPets); elementPets. name = (item. name ). toString (); // <xs: complexType> XmlSchemaComplexType complexType = new XmlSchemaComplexType (); elementPets. schemaType = complexType; // <xs: sequence> XmlSchemaSequence seqauth = new XmlSchemaSequence (); complexType. particle = seqauth; System. reflection. propertyInfo [] ps = item. getProperties (); for (int I = 0; I <ps. length; I ++) {// <xs: element name = ""/> XmlSchemaElement catRef = new XmlSchemaElement (); seqauth. items. add (catRef); catRef. name = ps [I]. name; string typeold = (ps [I]. propertyType ). toString (); string [] a = typeold. split ('. '); string typenew = (a [1]). toLower (); // Type format conversion if (typenew = "int32" | typenew = "int64") {typenew = "integer ";} if (typenew = "datetime") {typenew = "date";} catRef. schemaTypeName = new XmlQualifiedName (typenew ," http://www.w3.org/2001/XMLSchema ");} XmlSchemaSet schemaSet = new XmlSchemaSet (); schemaSet. add (schema); schemaSet. compile (); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet. schemas () {compiledSchema = schema1;} XmlNamespaceManager nsmgr = new XmlNamespaceManager (new NameTable (); nsmgr. addNamespace ("xs "," http://www.w3.org/2001/XMLSchema "); // Output and save using (MemoryStream stream = new MemoryStream () {compiledSchema. write (stream, nsmgr); FileStream fs = new FileStream (@ "E: \ XML \" + item. name. toString () + ". xml ", FileMode. create); BinaryWriter w = new BinaryWriter (fs); w. write (stream. toArray (); fs. close (); stream. close ();}}}
The result is as follows:
<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="DTO_BatchTotal"> <xs:complexType> <xs:sequence> <xs:element name="batch" type="xs:string" /> <xs:element name="total" type="xs:decimal" /> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
Reference: https://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema (v = vs.110). aspx
The two types of generation are all generated in batches using the entity set. You don't need to demonstrate a single entity.