XSD file generation C # VO object class,

Source: Internet
Author: User
Tags field table

XSD file generation C # VO object class,

Recently, the company wants to build a project that needs to be connected with other existing projects. since they do not know their database, only XSD files are available. Therefore, when modifying the corresponding program, we need to write our VO Object Class Based on the XSD file they provide. Since I have written a VO object class generated based on the Oracle database, therefore, this activity naturally falls on my head.

I. XSD

First, I will not explain what XSD is, because I am not very clear about it. For the first time, I have been familiar with it. For more details, I have mentioned that XSD is short for XML Schema Definition. Since it is XML, then, you can read the data in XML format.

Ii. resolved XSD File

The XSD file I want to parse looks like this. You can see that this file still needs to load two xsd files, but through our analysis, for our project, to generate an object class, in addition to parsing the XSD icon below, you also need to parse a type xsd, that is, the second xsd file in the include graph.

XSD to be parsed

Let's take a look at the above VO object class file. We can see that this VO object class contains many fields, but the type of each field is not expressed, therefore, you need to search in the xsd below, because the name in corresponds to the type, and the base in is the field type we need.

<Xs: simpleType name = "acbfyhdcbfyze"> <xs: annotation> <xs: documentation> total cost approved by cost </xs: documentation> </xs: annotation> <xs: restriction base = "xs: double"/> </xs: simpleType> <xs: simpleType name = "acbfyhdhdlrl"> <xs: annotation> <xs: documentation> approved profit margin by cost </xs: documentation> </xs: annotation> <xs: restriction base = "xs: double"/> </xs: simpleType> <xs: simpleType name = "acbfyhdhsdsre"> <xs: annotation> <xs: documentation> calculate the conversion amount by cost </xs: documentation> </xs: annotation> <xs: restriction base = "xs: double"/> </xs: simpleType> <xs: simpleType name = "acbfyhdynssde"> <xs: annotation> <xs: documentation> calculate the taxable income by cost </xs: documentation> </xs: annotation> <xs: restriction base = "xs: double"/> </xs: simpleType>Basic XSD

Iii. Analysis and Analysis

By analyzing the above two files, to generate object class files, the file to be parsed must be read successfully and a List will be generated. The basic XSD should be parsed into a Dictionary, in this Dictionary, you only need to include the name and base, and then replace them in the Dictionary by repeating the List to obtain the field type, to generate a List set that is ultimately required.

After analysis, it is put into practice. First, we must be able to parse the XSD file, parse XSD and parse XML, that is, get the node set, and then get what we want in a loop.

1. Set static fields based on nodes to facilitate calls during parsing

Public static class ConstField {public const string Schema = "xs: schema"; public const string ComplexType = "xs: complexType"; public const string Element = "xs: element "; public const string Annotation = "xs: annotation"; public const string Documentation = "xs: documentation"; public const string Sequence = "xs: sequence "; public const string SimpleType = "xs: simpleType"; public const string Restriction = "xs: restriction ";}Static Field

2. Write an object class that contains name (field name), type (field type), and note (annotation ).

Public class AnalysisVo {private string name; private string note; private string type; private List <AnalysisVo> children; /// <summary >/// Name /// </summary> public string name {get {return name;} set {Name = value ;}} /// <summary >/// remarks /// </summary> public string Note {get {return note;} set {note = value ;}} /// <summary >/// Type /// </summary> public string type {get {return type;} set {Type = value ;}} /// <summary >/// subset /// </summary> public List <AnalysisVo> Children {get {return children;} set {children = value ;}}}Entity class

3. parse the XSD File

Public void Analysis () {XmlElement rootElement = _ document. documentElement; // get the root node XmlNodeList complexTypeNodes = rootElement. getElementsByTagName (ConstField. complexType); // obtain the complexType subnode set foreach (XmlNode complexTypeNode in complexTypeNodes) {AnalysisVo vo = new AnalysisVo (); XmlElement voElement = (XmlElement) complexTypeNode; string nameVo = voElement. getAttribute ("name"); // obtain the class name string not EVo = voElement. firstChild. innerText; // obtain the annotation List of the class <AnalysisVo> childrenList = new List <AnalysisVo> (); XmlNodeList elementNodes = voElement. getElementsByTagName (ConstField. element); // obtain the element subnode set foreach (XmlNode elementNode in elementNodes) {AnalysisVo childVo = new AnalysisVo (); XmlElement fieldElement = (XmlElement) elementNode; string nameField = fieldElement. getAttribute ("name"); // obtain the field name str Ing typeField = fieldElement. GetAttribute ("type"); // obtain the field type string noteField = ""; // obtain the field annotation if (fieldElement. FirstChild! = Null) noteField = fieldElement. firstChild. innerText; childVo. name = nameField; childVo. type = typeField; childVo. note = noteField; childrenList. add (childVo);} vo. name = nameVo; vo. note = noteVo; vo. children = childrenList; VoList. add (vo );}}Parse object class XSD file public void Analysis () {XmlElement rootElement = _ document. documentElement; // get the root node XmlNodeList simpleTypeNodes = rootElement. getElementsByTagName (ConstField. simpleType); // obtain the simpleType subnode set foreach (XmlNode simpleTypeNode in simpleTypeNodes) {XmlElement dicElement = (XmlElement) simpleTypeNode; string dicKey = dicElement. getAttribute ("name"); // obtain the type name string dicValue = (XmlElement) dicElement. lastChild ). getAttribute ("base"); // obtain the type content dicValue = dicValue. replace ("xs:", ""); DicType. add (dicKey, dicValue );}}Parse Type XSD

4. Integrate the preceding two files to generate a final List file.

Private void ReplaceContent (AnalysisVo node) {// normally, if (DicType) exists in the basic field table. containsKey (node. type) {node. type = DicType [node. type];} // There is no type field in the field to be parsed, and only the name field yanc 20160304 else if (string. isNullOrEmpty (node. type) & DicType. containsKey (node. name) {node. type = DicType [node. name];} else // List type is not stored in the basic field table. You need to traverse its own List cyclically if (! String. isNullOrEmpty (node. type) & (node. type. substring (node. type. length-4, 4 ). equals ("Grid") | node. type. substring (node. type. length-4, 4 ). equals ("List") {foreach (var root in VoList) {if (root. name. equals (node. type) {node. type = root. children. first (). type ;}}// there is no type field in the field to be parsed, and only the name field yanc 20160304 else if (string. isNullOrEmpty (node. type) & (node. name. substring (node. name. length-4, 4 ). equals ("Grid") | node. name. substring (node. name. length-4, 4 ). equals ("List") {foreach (var root in VoList) {if (root. name. equals (node. name) {node. type = root. children. first (). name ;}} else {Console. writeLine ("unconverted content" + node. name );}}View Code

There is a situation in the replacement process, that is, an entity class contains a List <> generic set. I believe you have seen this situation during development. Therefore, during replacement, we need to judge that, through our analysis, this field type is generally not found in the base class, but is found in the current file. Therefore, when this happens, you need to loop through the set again to find the object set and assign its Name attribute to the current Type field.

4. Generate a CS File

The last step is to generate the CS file, which is similar to the previous article, but note that the entity class generated by the database does not have the List type, but this XSD generates, therefore, you need to slightly change the generation method.

Note: The preceding parsing method is also applicable to common request and response files, which must be processed separately.

V. Demo

An XSD generates multiple VO files. Each VO is an object class.

 

The code is on GitHub and I hope everyone can help with the modification. After all, the efficiency of foreach nested foreach is not very high.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.