从XSD生成C#类

从XSD生成C#类,c#,xsd,C#,Xsd,这是xml shema,我想从中使用xsd工具为C#生成类: <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:b2g="http://www.carina.hr/b2g/v1.0.0#" xmlns:xadesv141="http://uri.etsi.org/01903/v1.4.1#" xmlns:xades="http://uri.e

这是xml shema,我想从中使用xsd工具为C#生成类:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:b2g="http://www.carina.hr/b2g/v1.0.0#" xmlns:xadesv141="http://uri.etsi.org/01903/v1.4.1#" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.carina.hr/b2g/v1.0.0#" elementFormDefault="qualified">
   <xsd:import namespace="http://uri.etsi.org/01903/v1.4.1#" schemaLocation="http://uri.etsi.org/01903/v1.4.1/XAdESv141.xsd" />
   <xsd:import namespace="http://uri.etsi.org/01903/v1.3.2#" schemaLocation="http://uri.etsi.org/01903/v1.3.2/XAdES.xsd" />
   <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd" />
   <element name="B2GDocument" type="b2g:B2GDocumentType" />
   <complexType name="B2GDocumentType">
      <sequence>
         <element name="RequestHeader" type="b2g:RequestHeaderType" minOccurs="1" maxOccurs="1" />
         <element name="ResponseHeader" type="b2g:ResponseHeaderType" minOccurs="0" maxOccurs="1" />
         <element name="Content" type="b2g:ContentType" minOccurs="1" maxOccurs="1" />
         <xsd:element ref="ds:Signature" minOccurs="0" maxOccurs="1" />
      </sequence>
      <attribute name="version" type="string" fixed="1.0" />
   </complexType>
   <complexType name="RequestHeaderType">
      <annotation>
         <documentation>Header of the document.</documentation>
      </annotation>
      <sequence>
         <element name="AppId">
            <annotation>
               <documentation>Id of the target application.</documentation>
            </annotation>
            <simpleType>
               <restriction base="string">
                  <whiteSpace value="collapse" />
               </restriction>
            </simpleType>
         </element>
         <element name="TraderId">
            <annotation>
               <documentation>Id of the trader.</documentation>
            </annotation>
            <simpleType>
               <restriction base="string">
                  <xsd:maxLength value="17" />
                  <whiteSpace value="collapse" />
               </restriction>
            </simpleType>
         </element>
         <element name="TraderAppId">
            <annotation>
               <documentation>Id of the trader application.</documentation>
            </annotation>
            <simpleType>
               <restriction base="string">
                  <xsd:maxLength value="48" />
                  <whiteSpace value="collapse" />
               </restriction>
            </simpleType>
         </element>
         <element name="TraderMsgId" maxOccurs="1" minOccurs="0">
            <annotation>
               <documentation>Mime type of the document.
Currently:
text/xml application/pdf</documentation>
            </annotation>
            <simpleType>
               <restriction base="string" />
            </simpleType>
         </element>
         <element name="DocUuid" type="b2g:DocUuidType" maxOccurs="1" minOccurs="0" />
      </sequence>
      <xsd:attribute name="Id" type="xsd:ID" use="required" />
   </complexType>
   <complexType name="ContentType">
      <sequence>
         <element name="DocType" type="string" maxOccurs="1" minOccurs="1" />
         <element name="MimeType" type="string" maxOccurs="1" minOccurs="1" />
         <element name="Description" type="string" maxOccurs="1" minOccurs="0">
            <annotation>
               <documentation>Opcionalni opis dokumenta. Maksimalna duljina je 255 znakova.</documentation>
            </annotation>
         </element>
         <element name="Data" maxOccurs="1" minOccurs="1">
            <xsd:complexType mixed="true">
               <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                  <xsd:any namespace="##any" processContents="lax" />
               </xsd:sequence>
               <xsd:attribute name="encoding" use="required">
                  <simpleType>
                     <restriction base="string">
                        <enumeration value="EMBEDDED" />
                        <enumeration value="BASE64" />
                     </restriction>
                  </simpleType>
               </xsd:attribute>
               <xsd:anyAttribute namespace="##any" />
            </xsd:complexType>
         </element>
      </sequence>
      <xsd:attribute name="Id" type="xsd:ID" use="required" />
   </complexType>
   <complexType name="ResponseHeaderType">
      <sequence>
         <element name="DocUuid" type="b2g:DocUuidType" />
         <element name="ReceiveTimestamp" type="dateTime" />
      </sequence>
      <xsd:attribute name="Id" type="xsd:ID" use="required" />
   </complexType>
   <xsd:simpleType name="DocUuidType">
      <xsd:restriction base="xsd:string">
         <xsd:length value="36" />
         <xsd:pattern value="[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}" />
      </xsd:restriction>
   </xsd:simpleType>
</schema>
我得到这个信息:

Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.3038]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'http://www.w3.org/2000/09/xmldsig#:Signature' element is not declared. Line 20, position 11.

Warning: Schema could not be validated. Class generation may fail or may produce incorrect results.

Error: Error generating classes for schema 'G2BDokument'.
  - The element 'http://www.w3.org/2000/09/xmldsig#:Signature' is missing.

If you would like more help, please type "xsd /?".
我不知道我做错了什么,有人能帮我吗? 谢谢

如果您有此文件“xmldsig core schema.xsd”,请尝试以下操作:

C:\test>xsd G2BDokument.xsd xmldsig-core-schema.xsd/Classes

C:\test>xsd G2BDokument.xsd xmldsig-core-schema.xsd /nologo 

BR

几天前,我也遇到了类似的问题。在我的例子中,是构建XML文件的客户机出了问题。一段时间后,我决定简化我的方法。这可能不是最好的做法,但最终还是奏效了:

我有两个XSD:一个引用签名,另一个不引用签名。因为是客户机对XML文件进行签名,所以我在没有提及他的情况下给出了XSD。他创建了类,然后是XML,然后签名并发送给我。然后,我使用另一个XSD(带有引用的XSD)来检查一致性

事情似乎很顺利。
正如我所说,它不是最漂亮的,但它确实很好。呵呵。

谢谢它的工作性能

你们能解释一下nologo和class的区别吗

e、 x C:\test>xsd G2BDokument.xsd xmldsig-core-schema.xsd/Classes

C:\test>xsd G2BDokument.xsd xmldsig-core-schema.xsd /nologo 

我做了类似的事情,所以:必须传递2个参数。您的文件和参考资料。由于XSD.exe只接受1个输入参数,因此必须传递它.XSD路径

所以,你必须这样做:

  • 下载xmldsig-core-schema.xsd schema
  • 运行以下命令:

    “C:\Program Files\Microsoft SDK\Windows\Vxxx\bin\xsd.exe” [path_your_文件夹]\your_file.xsd [path_to_your_folder]\xmldsig-core-schema.xsd/c /n:mi\u新建\u类\u文件名/l:VB


  • 新文件“mi_new_class_file_name.vb”现在可能位于您的“C:\Program Files\Microsoft SDK\Windows\Vxxx\bin”文件夹中。

    您可以发布用于生成XSD的XML文件吗?我没有XML文件。我只有一些项目文档中的xsd。对于nologo,它没有显示以下信息:Microsoft(R)Xml模式/数据类型支持实用程序[Microsoft(R).NET Framework,版本4.0.30319.33440]版权所有(C)Microsoft Corporation。版权所有。