目录

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>

以文件方式保存在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>

然后打开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.

如何引用XML文件生成C#类的更多相关文章

  1. 根据XML文件 生成 java类

    最近一直在做关于webservice 的项目,这种项目最麻烦的就是根据对方的要求产生XML,文档里面虽然有XML结构,但是要转化为java里面的实体实在费劲, 有个自动化的工具就好了,半自动化也好,省 ...

  2. WebAPI使用多个xml文件生成帮助文档

    一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...

  3. WebAPI使用多个xml文件生成帮助文档(转)

    http://www.cnblogs.com/idoudou/p/xmldocumentation-for-web-api-include-documentation-from-beyond-the- ...

  4. 【转】WebAPI使用多个xml文件生成帮助文档

    来自:http://www.it165.net/pro/html/201505/42504.html 一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet ...

  5. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  6. C#读取XML文件的基类实现

    刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node ...

  7. XML文件与实体类的互相转换

    XML文件与实体类的互相转换 一.将XML文件反序列化为实体类对象 1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config).但是现在为了演示XML ...

  8. XML文件生成C++代码(基于rapidxml)

    简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...

  9. XML文件生成C++代码(基于pugixml)

    简述 在一个项目中需要用到XML的解析和生成,知乎上有人推荐rapidxml和pugixml等库.RapidXML一看库还比较大,就先研究一下pugixml了. 因为对解析XML的需求不大(都是一些很 ...

随机推荐

  1. C++ new(1)

    如果找工作的同学看一些面试的书,我相信都会遇到这样的题:sizeof 不是函数,然后举出一堆的理由来证明 sizeof 不是函数.在这里,和 sizeof 类似,new 和 delete 也不是函数, ...

  2. 同时屏蔽ios和android下点击元素时出现的阴影

    在ios4+和android2+系统,当手指触摸屏幕a标签链接或按钮时,会产生不同的效果,对于ios点击元素的时候,就会出现一个半透明的灰色背景:对于android则出现红色的边框.对这2个系统自带的 ...

  3. Varchar2 size how to decide?

    When you execute a complicate store procedure, maybe it will execute a long time, maybe you want to ...

  4. BI Project Managerment

    Design doc is the meta data of the code. The project management plan is crucial to your project sinc ...

  5. Redis安装,mongodb安装,hbase安装,cassandra安装,mysql安装,zookeeper安装,kafka安装,storm安装大数据软件安装部署百科全书

    伟大的程序员版权所有,转载请注明:http://www.lenggirl.com/bigdata/server-sofeware-install.html 一.安装mongodb 官网下载包mongo ...

  6. HDU 4454 Stealing a Cake --枚举

    题意: 给一个点,一个圆,一个矩形, 求一条折线,从点出发,到圆,再到矩形的最短距离. 解法: 因为答案要求输出两位小数即可,精确度要求不是很高,于是可以试着爆一发,暴力角度得到圆上的点,然后求个距离 ...

  7. UVALive 6255 Kingdoms --状态搜索

    题意:n个国家,给出国家间相互的债务关系,每个国家如果债务>收入就要破产,破产后该国的所有债务关系全部清除,第一个破产的国家不同有可能造成最后的没破产的国家的不同,问哪些国家有可能成为独自存活的 ...

  8. HOJ 2678 Stars

    题意:N个星星(x,y,z),星星的等级等于x,y,z都小于等于它的星星数量,问每个等级有多少星星. 思路:最暴力的方法是三维树状数组.但是会超内存.所以我们对其中一维先排好序,然后用二维的做. 代码 ...

  9. linux启动jmeter,执行./jmeter.sh报错解决方法

    1.l-bash: ./jmeter.sh: Permission denied解决办法:jmeter.sh的执行权限改改,是权限不够chmod 777 jmeter.sh 2.An error oc ...

  10. Linux安装telnet

    安装环境:CentOS 6.4    上篇已经讲述了memcached的安装,现在要测试Memcached功能的时候,需要使用到telnet服务.于是就有了本篇.   一.安装telnet 1.检测t ...