vb.net - 从 XSD 创建类并生成 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35285713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:10:43  来源:igfitidea点击:

vb.net - Create Class from XSD and generate xml

xmlvb.netxsd

提问by Twiebie

i have an xsd file and need to create an xml from it. Some pages assume to use the xsd.exe from Visual Studio. But how do I link the generated class to the xsd, to create xml files from it ?

我有一个 xsd 文件,需要从中创建一个 xml。某些页面假定使用 Visual Studio 中的 xsd.exe。但是如何将生成的类链接到 xsd,以从中创建 xml 文件?

Or is there another way to export values via the xsd schema to an xml file ?

还是有另一种方法可以通过 xsd 模式将值导出到 xml 文件?

回答by Mike Bateman

If you want to create a XML document that is based on a XSD there are a few steps you need to walk through.

如果要创建基于 XSD 的 XML 文档,则需要执行几个步骤。

1) You will need to create .NET classes based on your XSD.
2) You will need to create a new instance of that class and serialize the output.

1) 您需要根据您的 XSD 创建 .NET 类。
2) 您需要创建该类的新实例并序列化输出。

Step 1 - Create a .NET Class from a XSD Document

步骤 1 - 从 XSD 文档创建 .NET 类

A XSD file provides the blue print for a class. Here is a example of a XSD file:

XSD 文件提供类的蓝图。以下是 XSD 文件的示例:

<?xml version="1.0" encoding="utf-8"?>
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Person" nillable="true" type="Person" />
      <xs:complexType name="Person">
        <xs:sequence>
          <xs:element minOccurs="0" maxOccurs="1" name="firstName" type="xs:string" />
          <xs:element minOccurs="0" maxOccurs="1" name="lastName" type="xs:string" />
          <xs:element minOccurs="1" maxOccurs="1" name="dateOfBirth" type="xs:dateTime" />
          <xs:element minOccurs="1" maxOccurs="1" name="gender" type="Gender" />
          <xs:element minOccurs="1" maxOccurs="1" name="height" type="xs:int" />
          <xs:element minOccurs="1" maxOccurs="1" name="weight" type="xs:decimal" />
        </xs:sequence>
      </xs:complexType>
      <xs:simpleType name="Gender">
        <xs:restriction base="xs:string">
          <xs:enumeration value="Male" />
          <xs:enumeration value="Female" />
        </xs:restriction>
      </xs:simpleType>
</xs:schema>

Create a new folder to work in. I'm using 'C:\STACK'.
Create new text file, copy and paste the XSD into it and save it as 'person.xsd'.
Now we need to use the XSD.exe to convert this file into a class.
You will need to find the XSD exe on your machine, for me it was in:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\xsd.exe

创建一个新文件夹以在其中工作。我使用的是“C:\STACK”。
创建新的文本文件,将 XSD 复制并粘贴到其中并将其另存为“person.xsd”。
现在我们需要使用 XSD.exe 将这个文件转换成一个类。
你需要在你的机器上找到 XSD exe,对我来说它在:
C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\xsd.exe

Now open command prompt and enter this

现在打开命令提示符并输入这个

cd "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools"

Now we will create the .NET classes (here is is command broken down)

现在我们将创建 .NET 类(这里是命令分解)

xsd.exe             -Execute xsd   
/classes            -Create Clasess  
/language:vb        -Language to use (VB, CS, JS)  
/out:"c:\stack\"    -Output folder   
c:\stack\person.xsd -The XSD File to use  

Here is the command in one line

这是一行中的命令

xsd.exe c:\stack\person.xsd /classes /language:vb /out:c:\stack\  

After you have run this command a new file will be created 'c:\stack\person.vb' You can then add this class into you project.

运行此命令后,将创建一个新文件 'c:\stack\person.vb' 然后您可以将此类添加到您的项目中。

Step 2 - Create a new instance of that class and serialize the output

第 2 步 - 创建该类的新实例并序列化输出

Now that you have added the new class, you can create a instance of it:

现在您已经添加了新类,您可以创建它的一个实例:

    Dim person As New  Person
    person.firstName = "Mike"
    person.lastName = "Bateman"
    person.gender =  Gender.Male
    person.height = 160
    person.weight = 80.3

Now we can serialize the class to a XML file:

现在我们可以将类序列化为 XML 文件:

    Dim serializer As New XmlSerializer(GetType(Person))
    Dim writer As New StreamWriter("c:\stack\person.xml")
    serializer.Serialize(writer, person)
    writer.Close()

And we can read the XML back to a .NET class like this:

我们可以像这样将 XML 读回 .NET 类:

    Dim serializer As New XmlSerializer(GetType(Person))
    Dim reader As New IO.StreamReader("c:\stack\person.xml")
    Dim personRes As Person = serializer.Deserialize(reader)
    reader.Close()
    reader.Dispose()

Hope that helps!

希望有帮助!