zoukankan      html  css  js  c++  java
  • VS自带的xsd.exe工具,根据XML自动生成XSD

    Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD

    利用Vistual Studio自带的xsd.exe工具,根据XML自动生成XSD

    1, 命令提示符--》找到vs自带的xsd.exe工具所在的文件夹 例如: C:Program Files (x86)Microsoft SDKsWindowsv7.0ABin 注意:win7操作系统“命令提示符”要以管理员身份运行
    2,将Xml文件拷贝到xsd.exe工具所在的文件夹,生成的xsd文件也将在这个文件夹中
    3,在命令提示符中输入 xsd.exe test.xml, 多个xml文件以空格隔开

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

    xsd.exe语法示例

     一般情况下,xsd.exe位于C:/Program Files/Microsoft Visual Studio 8/SDK/v2.0/Bin

    1、将xsd文件自成.CS类库。

    正确写法: XSD.EXE xxx.xsd /l:c# /n:namespace

    xsd.exe   /d   /l:C#   a.xsd   /n:Namespace1.Namespace2

    /d   指令指示该工具生成   DataSet,

    /l:   告诉该工具要使用哪种语言(例如   C#   或   Visual   Basic   .NET)。可选的  

     /n:   指令指示该工具另外为   DataSet   生成名为   XSDSchema.Namespace   的命名空间。该命令的输出为   XSDSchemaFileName.cs

    2、  csc.exe   /t:library   XSDSchemaFileName.cs   /r:System.dll   /r:System.Data.dll     /t:   指令指示该工具编译成库,

    /r:   指令指定进行编译所需的依赖库。该命令的输出为   XSDSchemaFileName.dll,它可以在使用  

     /r:   指令编译   ADO.NET   应用程序时传递到编译器

    一、 如何将.xsn文件转成.cs文件。 

    用infopath打开.xsn文件,在文件-另存为源码,保存后,将会有一系列的文件,将myschema.xsd文件和xsd.exe文件放在同一目录下,在DOS窗口上运行:

    xsd.exe   /d   /l:C#   myschema.xsd   /n:Namespace1.Namespace2

    就会生成一个myschema.cs文件,部分代码如下:

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

    // <auto-generated>

    //     此代码由工具生成。

    //     运行库版本:2.0.50727.42

    //

    //     对此文件的更改可能会导致不正确的行为,并且如果

    //     重新生成代码,这些更改将会丢失。

    // </auto-generated>

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

    //

    // This source code was 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 {

    。。。。。。。。。。。

    二、Xsd

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

    或者xsd.exe   myschema.xsd /c /o:

    XmlSchema 类

    .NET Framework 2.0             
     
     

    按照 WWW 联合会 (W3C) XML 架构第 1 部分:“结构”和 XML 架构第 2 部分:“数据类型规范”内容指定的 XML 架构内存中表示形式。

    命名空间:System.Xml.Schema 程序集:System.Xml(在 system.xml.dll 中)

    复制
    public class XmlSchema : XmlSchemaObject
    
    复制
    public class XmlSchema extends XmlSchemaObject
    
    复制
    public class XmlSchema extends XmlSchemaObject
    
    Security note安全注意

    使用 XmlSchema 类会引发异常,如 XmlSchemaException 类可能包含不应在不可信方案中公开的敏感信息。例如,XmlSchemaException 的 SourceUri 属性返回的架构文件的 URI 路径导致异常。SourceUri 属性不应在不受信任的情况下公开。应该正确处理异常以便此敏感信息不会在不受信任的情况下公开。

    下面的示例创建了一个架构定义。

    复制
    using System;
    using System.Xml;
    using System.Xml.Schema;
    
    class XMLSchemaExamples
    {
        public static void 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);
        }
    }
    
    

    下面的 XML 文件是为前面的代码示例生成的。

    复制
    <?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>
    
    
    System.Object    System.Xml.Schema.XmlSchemaObject     System.Xml.Schema.XmlSchema
     
  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Symmetric Tree
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Triangle
    Populating Next Right Pointers in Each Node II
    Pascal's Triangle II
    Pascal's Triangle
    Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/14498924.html
Copyright © 2011-2022 走看看