我怎样才能做到这一点? [英] How Can I achieve this ?

查看:60
本文介绍了我怎样才能做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi 

请问您如何从多个xsd文件中生成C#中的xml文件?下面是我的两个xsd文件的格式。而且我还需要确保生成的xml通过xsd验证。

Could you please tell me how can I generate xml file in C# from multiple xsd file ? Below is the format of my two xsd files . and I also I need to make sure that the xml generated gets validated with the xsd .

EmployeeDetailsEvent.xsd

<?xml version =" 1.0" encoding =" UTF-8"?>



- < xs:schema attributeFormDefault =" unqualified"将elementFormDefault = QUOT;合格"的targetNamespace = QUOT; HTTP://xmlns.geg.com/api/Gegroup/EmployeeDetailsEvent/1.0"的xmlns:XS = QUOT; HTTP://www.w3.org/2001/XMLSchema"的xmlns:TNS = QUOT; HTTP://xmlns.geg.com/api/Gegroup/EmployeeDetailsEvent/1.0"
xmlns:EmployeeDetails =" http://xmlns.geg.com/Gegroup/EmployeeDetails/1.0">



< xs:import的schemaLocation = QUOT; /home/GEgroup/Asia/ge/schema/.EmployeeDetails.0.xsd" namespace =" http://xmlns.geg.com/Gegroup/EmployeeDetails/1.0" />
$




- < ; xs:element name =" EmployeeDetailsEvent">



$
- < xs:complexType>



$
- < xs:sequence>





- < ; xs:element name =" EmployeeDetails">



$
- < xs:complexType>



$
- < xs:sequence>



< xs:element name =" EmployeeDetails" ; maxOccurs的= QUOT;无界"的minOccurs = QUOT 1 QUOT; type =" EmployeeDetails:EmployeeDetailsType" />



< / xs:sequence>

$
< / xs:complexType>
$


< / xs:element>



< / xs :sequence>
$


< / xs:complexType>



< / xs:element>



< / xs:schema>

<?xml version="1.0" encoding="UTF-8"?>

-<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://xmlns.geg.com/api/Gegroup/EmployeeDetailsEvent/1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://xmlns.geg.com/api/Gegroup/EmployeeDetailsEvent/1.0" xmlns:EmployeeDetails="http://xmlns.geg.com/Gegroup/EmployeeDetails/1.0">

<xs:import schemaLocation="/home/GEgroup/Asia/ge/schema/.EmployeeDetails.0.xsd" namespace="http://xmlns.geg.com/Gegroup/EmployeeDetails/1.0"/>


-<xs:element name="EmployeeDetailsEvent">


-<xs:complexType>


-<xs:sequence>


-<xs:element name="EmployeeDetails">


-<xs:complexType>


-<xs:sequence>

<xs:element name="EmployeeDetails" maxOccurs="unbounded" minOccurs="1" type="EmployeeDetails:EmployeeDetailsType"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

EmployeeDetails.xsd

推荐答案

一般过程如下:

The general process is as follows:

1)使用XSD.EXE命令从XSD文件生成C#类。 XSD.exe的文档在这里:

https://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe

1) Use the XSD.EXE command to generate a C# class from your XSD file. The documentation for XSD.exe is here: https://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-schema-definition-tool-xsd-exe

2)将生成的.cs文件添加到包含您希望用于生成XML文件的代码的项目中。

2) Add the generated .cs file to the project that contains the code that you wish to use for generating the XML file.

3)在项目中编写创建实例的代码生成的类并将值分配给其属性。这些值将写入XML文件。

3) Write code in your project that creates an instance of the generated class and assigns values to its properties. These values will be written into the XML file.

4)使用XmlSerializer类将类序列化为文件。

4) Use the XmlSerializer class to serialize the class into a file.

就是这样。您刚刚编写的XML文件将针对起始XSD传递验证。

That's it. The XML file that you just wrote will pass validation against the starting XSD.

警告:当架构在单个XSD文件中时,这是有效的,但XSD.EXE不知道如何组合像你一样有两个独立的XSD文件。我担心你需要先手动编辑你的XDS文件,然后在上面的步骤1开始
之前将它们组合成一个XSD。这当然需要对XSD有足够的了解,你不能盲目地将一个文件的内容复制并粘贴到另一个文件中。

Caveat: This works when the schema is in a single XSD file, but XSD.EXE does not know how to combine two separate XSD files like you have. I'm afraid that you will need to first edit your XDS files manually to combine them into a single XSD before starting with step 1 above. This of course requires a sufficient understanding of the XSD, you can't just blindly copy and paste the content of one file into the other.


这篇关于我怎样才能做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆