Automatically generate XSD from XML using the Xsd.exe tool that comes with Vistual studio

Source: Internet
Author: User

automatically generate XSD from XML using the Xsd.exe tool that comes with Vistual studio

1, Command Prompt-"Find the folder where the VS comes with the Xsd.exe tool for example: C:\Program Files (x86) \microsoft Sdks\windows\v7.0a\bin Note: Win7 operating system" command Prompt " To run as Administrator
2. Copy the XML file to the folder where the Xsd.exe tool is located, and the resulting XSD file will be in this folder
3, enter Xsd.exe test.xml at the command prompt, and multiple XML files are separated by spaces

===============================================

Xsd.exe syntax examples

In general, Xsd.exe is located in C:/Program files/microsoft Visual Studio 8/sdk/v2.0/bin

1. Make the XSD file self-made. CS Class Library.

Correct wording: XSD. EXE xxx.xsd/l:c#/n:namespace

XSD.EXE/D/l:c# A.xsd/n:namespace1.namespace2

The/d directive instructs the tool to generate a DataSet,

/L: Tell the tool which language to use, such as C # or Visual Basic. NET). An optional

The/N: Directive instructs the tool to also generate a namespace named Xsdschema.namespace for the DataSet. The output of this command is XSDSchemaFileName.cs

2. Csc.exe/t:library xsdschemafilename.cs/r:system.dll/r:system.data.dll/t: Instruction instructs the tool to compile into a library,

The/r: directive specifies the dependent libraries required for compilation. The output of this command is XSDSchemaFileName.dll, which can be used in the

/r: Instructions are passed to the compiler when compiling the ADO application

First, how to convert. xsn files to. cs files.

Open the. xsn file with InfoPath, in the file-Save as source, after saving, there will be a series of files, the Myschema.xsd file and the Xsd.exe file in the same directory, running on a DOS window:

XSD.EXE/D/l:c# Myschema.xsd/n:namespace1.namespace2

A Myschema.cs file is generated and some of the code is as follows:

//------------------------------------------------------------------------------

<auto-generated>

// This code is generated by the tool.

// runtime version : 2.0.50727.42

//

// changes to this file may result in incorrect behavior, and if

// Regenerate the code, these changes will be lost.

</auto-generated>

//------------------------------------------------------------------------------

//

This source code is auto-generated by XSD, version=2.0.50727.42.

//

Namespace Namespace1.namespace2 {

Using System;

[System.CodeDom.Compiler.GeneratedCodeAttribute ("System.Data.Design.TypedDataSetGenerator", "2.0.0.0")]

[Serializable ()]

[System.ComponentModel.DesignerCategoryAttribute ("code")]

[System.ComponentModel.ToolboxItem (True)]

[System.Xml.Serialization.XmlSchemaProviderAttribute ("Gettypeddatasetschema")]

[System.Xml.Serialization.XmlRootAttribute ("NewDataSet")]

[System.ComponentModel.Design.HelpKeywordAttribute ("Vs.data.DataSet")]

Public partial class NewDataSet:System.Data.DataSet {

。。。。。。。。。。。

Second, XSD

Xsd.exe myschema.xsd/c/o: "C:/infopath"

or Xsd.exe myschema.xsd/c/o:

XmlSchema class . NET Framework 2.0Other versions
    • . NET Framework 4.5
    • . NET Framework 4
    • . NET Framework 3.5
    • . NET Framework 3.0
    • Silverlight

The XML Schema in-memory representation specified in the "Structure" and the XML Schema part 2nd: "Data type Specification" is described in accordance with the WWW Consortium XML Schema Part 1th.

namespaces: System.Xml.Schema Assembly:System.Xml (in System.Xml.dll)

Grammar C#C++VB replication
Class Xmlschema:xmlschemaobject
J # Replication
public class XmlSchema extends XmlSchemaObject
JScript Replication
Extends XmlSchemaObject
Notes
Safety note

Using the XmlSchema class throws an exception, such as a xmlschemaexception class that may contain sensitive information that should not be exposed in untrusted scenarios. For example, thexmlschemaexception SourceUri property returns the URI path of the schema file that caused the exception. The SourceUri property should not be exposed in untrusted situations. Exceptions should be handled correctly so that this sensitive information is not exposed in untrusted situations.

Example

The following example creates a schema definition.

C#VB replication
Using System;Using System.Xml;Using System.Xml.Schema;Class xmlschemaexamples{PublicStaticvoid Main () {XmlSchema schema =New XmlSchema ();<xs:element name= "Cat" type= "xs:string"/> xmlschemaelement elementcat =New XmlSchemaElement (); Schema. Items.Add (ELEMENTCAT); Elementcat.name ="Cat"; Elementcat.schematypename =New XmlQualifiedName ("String","Http://www.w3.org/2001/XMLSchema");<xs:element name= "Dog" type= "xs:string"/> xmlschemaelement elementdog =New XmlSchemaElement (); Schema. Items.Add (Elementdog); Elementdog.name ="Dog"; Elementdog.schematypename =New XmlQualifiedName ("String","Http://www.w3.org/2001/XMLSchema");<xs:element name= "Reddog" substitutiongroup= "dog"/> xmlschemaelement elementreddog =New XmlSchemaElement (); Schema. Items.Add (Elementreddog); Elementreddog.name ="Reddog"; Elementreddog.substitutiongroup =New XmlQualifiedName ("Dog");<xs:element name= "Browndog" substitutiongroup = "dog"/> xmlschemaelement elementbrowndog =New XmlSchemaElement (); Schema. Items.Add (Elementbrowndog); Elementbrowndog.name ="Browndog"; Elementbrowndog.substitutiongroup =New XmlQualifiedName ("Dog");<xs:element name= "Pets" > XmlSchemaElement elementpets =New XmlSchemaElement (); Schema. Items.Add (elementpets); Elementpets.name ="Pets";<xs:complexType> Xmlschemacomplextype ComplexType =New Xmlschemacomplextype (); Elementpets.schematype = ComplexType;<xs:choice minoccurs= "0" maxoccurs= "unbounded" > Xmlschemachoice choice =New Xmlschemachoice (); complextype.particle = choice; Choice. MinOccurs = 0; Choice. Maxoccursstring ="Unbounded";<xs:element ref= "Cat"/> xmlschemaelement catref =New XmlSchemaElement (); Choice. Items.Add (CATREF); Catref.refname =New XmlQualifiedName ("Cat");<xs:element ref= "Dog"/> xmlschemaelement dogref =New XmlSchemaElement (); Choice. Items.Add (DOGREF); Dogref.refname =New XmlQualifiedName ("Dog"); XmlSchemaSet Schemaset =New XmlSchemaSet (); Schemaset.validationeventhandler + =new ValidationEventHandler (Validationcallbackone); 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"); Compiledschema.write (Console.Out, nsmgr);} public static void Validationcallbackone (object sender, ValidationEventArgs args) { Console.WriteLine (args. Message); }}

The following XML file is generated for the preceding code example.

None copy
<?xml version= "1.0" encoding= "IBM437"? ><xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema" >    <xs:element name= "Cat" type= "xs:string"/>    <xs:element name= "dog" type= "xs:string"/>    <xs: Element name= "Reddog" type= "xs:string" substitutiongroup= "dog"/>    <xs:element name= "Browndog" type= "xs: string "SubstitutionGroup =" dog "/>    <xs:element name=" Pets ">      <xs:complexType>        <xs: Choice minoccurs= "0" maxoccurs= "unbounded" >          <xs:element ref= "cat"/> <xs:element          ref= "Dog"/>        </xs:choice>      </xs:complexType>    </xs:element></xs:schema>
Inheritance Hierarchy System.Object System.Xml.Schema.XmlSchemaObject System.Xml.Schema.XmlSchema

Automatically generate XSD from XML using the Xsd.exe tool that comes with Vistual studio

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.