Generate C # classes based on XSD

Source: Internet
Author: User

varFile ="1.xsd"; //Get The namespace for the schema.CodeNamespace ns = processor.process (file,"Dm"); //Create the appropriate generator for the language.CodeDomProvider provider; if("CS"=="CS") provider=NewMicrosoft.CSharp.CSharpCodeProvider (); Else if(args[3] =="vb") provider=NewMicrosoft.VisualBasic.VBCodeProvider (); Else                Throw NewArgumentException ("Invalid Language", args[3]); //Write The code to the output file.            using(StreamWriter SW =NewStreamWriter (file,false) {provider. CreateGenerator (). GenerateCodeFromNamespace (NS, SW,Newcodegeneratoroptions ()); } Console.WriteLine ("finished"); Console.read ();

Process

 Public Sealed classProcessor { Public StaticCodeNamespace Process (stringXsdfile,stringtargetnamespace) {                //Load the XmlSchema and its collection.XmlSchema xsd; using(FileStream fs =NewFileStream (Xsdfile, FileMode.Open)) {xsd= Xmlschema.read (FS,NULL); Xsd.compile (NULL); } XmlSchemas Schemas=NewXmlSchemas (); Schemas.                ADD (XSD); //Create The importer for these schemas.XmlSchemaImporter importer =Newxmlschemaimporter (schemas); //system.codedom namespace for the XmlCodeExporter to put classes in.CodeNamespace NS =NewCodeNamespace (targetnamespace); XmlCodeExporter exporter=NewXmlCodeExporter (NS); //iterate schema top-level elements and export code for each.                foreach(xmlschemaelement Elementinchxsd. Elements.values) {//Import the mapping first.XmlTypeMapping mapping =importer. ImportTypeMapping (element.                    QualifiedName); //Export the code finally.exporter.                Exporttypemapping (mapping); }                returnNS; }        } 

Client

Generate C # classes based on XSD

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.