从 XSD 生成 C# 类

时间:2023-03-28
本文介绍了从 XSD 生成 C# 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

This is xml shema from which I want generate classes for C# using xsd tool:

<?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>

当我尝试使用

C:	est>xsd G2BDokument.xsd /Classes

我收到这条消息:

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 /?".

我无法弄清楚我做错了什么,所以任何人都可以帮助我吗?谢谢!

I can not figure out what I am doing wrong, so can anyone please help me? Thanks!

推荐答案

如果你有这个文件xmldsig-core-schema.xsd",试试这个:

if you have this file "xmldsig-core-schema.xsd", try this:

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

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

BR

这篇关于从 XSD 生成 C# 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:c# XML Schema 验证 下一篇:C# HttpClient 4.5 多部分/表单数据上传

相关文章