zoukankan      html  css  js  c++  java
  • 如何引用XML文件生成C#类

     

    目录

    XSD File Generate Class File Simply. 1

    Why use XSD file to create C# classes?... 2

    How to convert XML file to XSD file?. 2

    How to create C# file by XSD file?. 4

    What is xsd command params?. 8

    Reference. 11

     

     

    Why use XSD file to create C# classes?

    当应用场景是这样的:

    1.    需要定义大量的自定义格式的XML协议,供通讯双方使用,本文基于内部接口测试工具的测试协议格式。

    2.    减少中间依赖关系,类依赖于XML协议的格式,而非用CS文件序列化XML文件。

    How to convert XML file to XSD file?

    准备一个支持W3C标准的XML文件(必须有文件头<?xml version="1.0" encoding="utf-8"?>)

    <?xml version="1.0" encoding="utf-8" ?>
    
    <protobuftest>
    
             <head>
    
                       <version>1.0.0</version>
    
                       <description>Bocode protobuf test protocol</description>
    
             </head>
    
             <transaction>
    
                       <name>Transaction 01</name>
    
                       <items>
    
                                <item>
    
                                         <name>Functional Test 01</name>
    
                                         <description>Test protobuf demo</description> 
    
                                         <testinput>
    
                                                   <code>4002</code>
    
                                         </testinput>
    
                                         <testparams> 
    
                                                   <usepretest>false</usepretest>
    
                                
    
                                                   <keystring>
    
                                                            <key>10005</key>
    
                                                            <value>100</value>
    
                                                            <pindex>0</pindex>
    
                                                   </keystring>
    
                       
    
                                                   <keybytes>
    
                                                            <key>10007</key>
    
                                                            <value>12jkjk123jk12312k312k3k1221k3jk21123213k3k13k1</value>
    
                                                            <pindex>0</pindex>
    
                                                   </keybytes>
    
                                
    
                                                   <keylist>
    
                                                            <keystring>
    
                                                                     <key>30002</key>                   
    
                                                                     <value>0</value>
    
                                                                     <pindex>0</pindex>
    
                                                            </keystring>
    
                                                            <keystring>
    
                                                                     <key>30002</key>
    
                                                                     <value>10</value>
    
                                                                     <pindex>0</pindex>
    
                                                            </keystring>
    
                                                            <keystring>
    
                                                                     <key>30002</key>
    
                                                                     <value>10</value>
    
                                                                     <pindex>0</pindex>
    
                                                            </keystring>
    
                                                   </keylist>
    
                                                   </testparams>
    
                                         <testoutput>
    
                                                   <resultcode>0</resultcode> 
    
                                         </testoutput>
    
                                </item> 
    
                                <item>
    
                                         <name>Functional Test 02</name>
    
                                         <description>Test protobuf demo</description>
    
                                         <testinput>
    
                                                   <code >4002</code>
    
                                         </testinput>
    
                                         <testparams> 
    
                                                   <keystring>
    
                                                            <key>10005</key>
    
                                                            <value>100</value>
    
                                                   </keystring>
    
                                                   <keybytes>
    
                                                            <key>10007</key>
    
                                                            <value>12jkjk123jk12312k312k3k1221k3jk21123213k3k13k1</value>
    
                                                   </keybytes>
    
                                                   <keylist>
    
                                                            <keystring>
    
                                                                     <key>30002</key>
    
                                                                     <value>0</value>
    
                                                            </keystring>
    
                                                            <keystring>
    
                                                                     <key>30002</key>
    
                                                                     <value>10</value>
    
                                                            </keystring>
    
                                                            <keystring>
    
                                                                     <key>30002</key>
    
                                                                     <value>10</value>
    
                                                            </keystring>
    
                                                   </keylist>
    
                                         </testparams>
    
                                         <testoutput>
    
                                                   <resultcode>0</resultcode> 
    
                                         </testoutput>
    
                                </item> 
    
                       </items>
    
             </transaction>
    
    </protobuftest>
    View Code

    以文件方式保存在XXX路径下,并启动VS2012 ARM Cross Tools Command Prompt

     

    之后在XXX目录下即生成ProtobufTest_Demo.xsd文件

     

    How to create C# file by XSD file?

    首先准备一个xsd文件,保存在XXX路径,命名为ProtobufTest_Demo.xsd

    <?xml version="1.0" encoding="utf-8"?>
    
    <xs:schema id="protobuftest" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    
      <xs:element name="keystring">
    
        <xs:complexType>
    
          <xs:sequence>
    
            <xs:element name="key" type="xs:string" minOccurs="0" />
    
            <xs:element name="value" type="xs:string" minOccurs="0" />
    
            <xs:element name="pindex" type="xs:string" minOccurs="0" />
    
          </xs:sequence>
    
        </xs:complexType>
    
      </xs:element>
    
      <xs:element name="protobuftest" msdata:IsDataSet="true" msdata:Locale="en-US">
    
        <xs:complexType>
    
          <xs:choice minOccurs="0" maxOccurs="unbounded">
    
            <xs:element ref="keystring" />
    
            <xs:element name="head">
    
              <xs:complexType>
    
                <xs:sequence>
    
                  <xs:element name="version" type="xs:string" minOccurs="0" />
    
                  <xs:element name="description" type="xs:string" minOccurs="0" />
    
                </xs:sequence>
    
              </xs:complexType>
    
            </xs:element>
    
            <xs:element name="transaction">
    
              <xs:complexType>
    
                <xs:sequence>
    
                  <xs:element name="name" type="xs:string" minOccurs="0" />
    
                  <xs:element name="items" minOccurs="0" maxOccurs="unbounded">
    
                    <xs:complexType>
    
                      <xs:sequence>
    
                        <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
    
                          <xs:complexType>
    
                            <xs:sequence>
    
                              <xs:element name="name" type="xs:string" minOccurs="0" />
    
                              <xs:element name="description" type="xs:string" minOccurs="0" />
    
                              <xs:element name="testinput" minOccurs="0" maxOccurs="unbounded">
    
                                <xs:complexType>
    
                                  <xs:sequence>
    
                                    <xs:element name="code" type="xs:string" minOccurs="0" />
    
                                  </xs:sequence>
    
                                </xs:complexType>
    
                              </xs:element>
    
                              <xs:element name="testparams" minOccurs="0" maxOccurs="unbounded">
    
                                <xs:complexType>
    
                                  <xs:sequence>
    
                                    <xs:element name="usepretest" type="xs:string" minOccurs="0" />
    
                                    <xs:element ref="keystring" minOccurs="0" maxOccurs="unbounded" />
    
                                    <xs:element name="keybytes" minOccurs="0" maxOccurs="unbounded">
    
                                      <xs:complexType>
    
                                        <xs:sequence>
    
                                          <xs:element name="key" type="xs:string" minOccurs="0" />
    
                                          <xs:element name="value" type="xs:string" minOccurs="0" />
    
                                          <xs:element name="pindex" type="xs:string" minOccurs="0" />
    
                                        </xs:sequence>
    
                                      </xs:complexType>
    
                                    </xs:element>
    
                                    <xs:element name="keylist" minOccurs="0" maxOccurs="unbounded">
    
                                      <xs:complexType>
    
                                        <xs:sequence>
    
                                          <xs:element ref="keystring" minOccurs="0" maxOccurs="unbounded" />
    
                                        </xs:sequence>
    
                                      </xs:complexType>
    
                                    </xs:element>
    
                                  </xs:sequence>
    
                                </xs:complexType>
    
                              </xs:element>
    
                              <xs:element name="testoutput" minOccurs="0" maxOccurs="unbounded">
    
                                <xs:complexType>
    
                                  <xs:sequence>
    
                                    <xs:element name="resultcode" type="xs:string" minOccurs="0" />
    
                                  </xs:sequence>
    
                                </xs:complexType>
    
                              </xs:element>
    
                            </xs:sequence>
    
                          </xs:complexType>
    
                        </xs:element>
    
                      </xs:sequence>
    
                    </xs:complexType>
    
                  </xs:element>
    
                </xs:sequence>
    
              </xs:complexType>
    
            </xs:element>
    
          </xs:choice>
    
        </xs:complexType>
    
      </xs:element>
    
    </xs:schema>
    View Code

    然后打开VS2012 Command Prompt(开始---所有程序---Microsoft Visual Studio 2012---Visual Studio Tools---VS2012 ARM Cross Tools Command Prompt)

     

    (文中TestXSD为ProtobufTest_Demo.xsd路径, /classes为输出类型, /language:CS为输出语言)

     

    打开 ProtobufTest_Demo.cs文件:

     

    What is xsd command params?

    C:TestXSD>xsd /?
    
    Microsoft (R) Xml Schemas/DataTypes support utility
    
    [Microsoft (R) .NET Framework, Version 4.0.30319.17929]
    
    Copyright (C) Microsoft Corporation. All rights reserved.
    
     
    
    xsd.exe -
    
        Utility to generate schema or class files from given source.
    
     
    
    xsd.exe <schema>.xsd /classes|dataset [/e:] [/l:] [/n:] [/o:] [/s] [/uri:]
    
    xsd.exe <assembly>.dll|.exe [/outputdir:] [/type: [...]]
    
    xsd.exe <instance>.xml [/outputdir:]
    
    xsd.exe <schema>.xdr [/outputdir:]
    
     
    
         - OPTIONS -
    
     
    
    /classes
    
        Generate classes for this schema. Short form is '/c'.
    
     
    
    /dataset
    
        Generate sub-classed DataSet for this schema. Short form is '/d'.
    
     
    
    /enableLinqDataSet
    
        Generate LINQ-enabled sub-classed Dataset for the schemas provided.  Short f
    
    orm is '/eld'.
    
     
    
    /element:<element>
    
        Element from schema to process. Short form is '/e:'.
    
     
    
    /fields
    
        Generate fields instead of properties. Short form is '/f'.
    
     
    
    /order
    
        Generate explicit order identifiers on all particle members.
    
     
    
    /enableDataBinding
    
        Implement INotifyPropertyChanged interface on all generated types
    
        to enable data binding. Short form is '/edb'.
    
     
    
    /language:<language>
    
        The language to use for the generated code. Choose from 'CS', 'VB', 'JS',
    
        'VJS', 'CPP' or provide a fully-qualified name for a class implementing
    
        System.CodeDom.Compiler.CodeDomProvider. The default language
    
        is 'CS' (CSharp). Short form is '/l:'.
    
     
    
    /namespace:<namespace>
    
        The namespace for generated class files. The default namespace
    
        is the global namespace. Short form is '/n:'.
    
     
    
    /nologo
    
        Suppresses the banner.
    
     
    
    /out:<directoryName>
    
        The output directory to create files in. The default
    
        is the current directory. Short form is '/o:'.
    
     
    
    /type:<type>
    
        Type from assembly to generate schema for. Multiple types may be provided.
    
        If no types are provided, then schemas for all types in an assembly
    
        are generated. Short form is '/t:'.
    
     
    
    /uri:<uri>
    
        Uri of elements from schema to process. Short form is '/u:'.
    
     
    
         - ADVANCED -
    
     
    
    /parameters:<file>
    
        Read command-line options from the specified xml file. Short form is '/p:'.
    
     
    
         - ARGUMENTS -
    
    <schema>.xsd       Name of a schema containing elements to import.
    
    <assembly>.dll|exe Name of an assembly containing types to generate schema for.
    
    <instance>.xml     Name of an xml file to infer xsd schema from.
    
    <schema>.xdr       Name of an xdr schema to convert to xsd.
    
    Multiple file arguments of the same type may be provided.

    Reference  

    http://msdn.microsoft.com/zh-cn/library/x6c1kb0s(VS.80).aspx.

     

     

  • 相关阅读:
    [LeetCode] 39. Combination Sum 组合之和
    CSS3
    常见中文字体在CSS中的Unicode编码(宋体:5B8B4F53)
    List<Object> 使用Linq
    查看工作流详情页面
    java程序调用.net接口服务地址的写法
    C# Repeater 嵌套
    JavaScript刷新页面,不重复提交
    Migration-添加表(加外键)
    Migration-添加表
  • 原文地址:https://www.cnblogs.com/cuiyansong/p/3741466.html
Copyright © 2011-2022 走看看