net -从XSD创建类并生成xml

时间:2023-01-15 17:11:02

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。有些页面假设使用xsd。从Visual Studio exe。但是,如何将生成的类链接到xsd,以从中创建xml文件呢?

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

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

1 个解决方案

#1


6  

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类。您将需要创建该类的一个新实例并序列化输出。

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:\栈”。创建新的文本文件,将XSD复制并粘贴到其中,并将其保存为“person.xsd”。现在我们需要使用XSD。exe将该文件转换为类。你需要找到XSD exe在您的机器上,对我来说,这是在:C:\Program Files (x86)\Microsoft sdk \ Windows \ v8.0A \ bin \ \ xsd.exe NETFX 4.0工具

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:\栈\person”。然后你可以将这个类添加到你的项目中。

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!

希望会有帮助!

#1


6  

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类。您将需要创建该类的一个新实例并序列化输出。

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:\栈”。创建新的文本文件,将XSD复制并粘贴到其中,并将其保存为“person.xsd”。现在我们需要使用XSD。exe将该文件转换为类。你需要找到XSD exe在您的机器上,对我来说,这是在:C:\Program Files (x86)\Microsoft sdk \ Windows \ v8.0A \ bin \ \ xsd.exe NETFX 4.0工具

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:\栈\person”。然后你可以将这个类添加到你的项目中。

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!

希望会有帮助!