如何从xsd生成.NET 4.0类?

时间:2011-03-07 08:58:07

标签: c# .net xsd

使用Visual Studio 2010从xsd文件生成.NET 4.0 c#类(实体)有哪些选项?

11 个答案:

答案 0 :(得分:443)

很简单;只需运行(在vs命令提示符下)

xsd your.xsd /classes

(将创建your.cs)。但请注意,自2.0以来,此处的大部分内在选项都没有太大变化。

对于选项,请使用xsd /?see MSDN;例如/enableDataBinding可能很有用。

答案 1 :(得分:53)

Marc Gravell提到的xsd.exe。启动和运行IMO的最快方法。

或者如果您需要更多灵活性/选项:

xsd2code VS加载项(Codeplex)

答案 2 :(得分:14)

当你有循环引用时,xsd.exe不能正常工作(即一个类型可以直接或间接拥有自己类型的元素)。

当存在循环引用时,我使用Xsd2Code。 Xsd2Code很好地处理循环引用,并在VS IDE中工作,这是一个很大的优点。它还具有许多功能,例如生成序列化/反序列化代码。如果您正在生成序列化,请确保打开GenerateXMLAttributes(否则如果未在所有元素上定义,您将获得排序异常)。

两者都不适用于选择功能。你最终会得到对象的列表/集合而不是你想要的类型。如果可能的话,我建议在xsd中避免选择,因为这不会很好地序列化/反序列化为强类型类。如果你不关心这个,那么这不是问题。

xsd2code中的任何功能反序列化为System.Xml.XmlElement,我觉得这很方便但如果你想要强类型对象可能会出问题。我经常在允许自定义配置数据时使用any,因此XmlElement可以方便地传递给另一个在别处自定义的XML反序列化器。

答案 3 :(得分:7)

我在这里向您展示使用Vs2017和Vs2019的最简单方法 使用Visual Studio打开xsd,并按照建议的url生成示例XML文件。

  1. 按如下所示在设计视图中打开xsd后,请单击xml模式资源管理器。 enter image description here

2。  在“ XML Schema Explorer”中,一直向下滚动以找到根/数据节点。右键单击根/数据节点,它将显示“ Generate Sample XML”。如果未显示,则表示您不在数据元素节点上,但在任何数据定义节点上。

enter image description here

  1. 将生成的Xml复制到剪贴板中
  2. 在解决方案中创建一个新的空类,然后删除类定义。应该只保留命名空间
  3. 将鼠标指针放在班级内部时,选择 EDIT-> Pase Special-> Paste Xml as Classes

答案 4 :(得分:4)

我在批处理脚本中使用XSD直接从.xsd生成XML个文件和类:

set XmlFilename=Your__Xml__Here
set WorkingFolder=Your__Xml__Path_Here

set XmlExtension=.xml
set XsdExtension=.xsd

set XSD="C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1\Tools\xsd.exe"

set XmlFilePath=%WorkingFolder%%XmlFilename%%XmlExtension%
set XsdFilePath=%WorkingFolder%%XmlFilename%%XsdExtension%

%XSD% %XmlFilePath% /out:%WorkingFolder%
%XSD% %XsdFilePath% /c /out:%WorkingFolder%

答案 5 :(得分:1)

Marc Gravells的回答对我来说是正确的,但我的xsd是扩展名.xml。当我使用xsd程序时,它给出了:
- The table (Amt) cannot be the child table to itself in nested relations.

根据这个KB325695,我将.xml的扩展名重命名为.xsd,并且运行正常。

答案 6 :(得分:1)

对于快速且懒惰的解决方案,(并且完全不使用VS)请尝试以下在线转换器:

  • xsd-to-xml-converter here
  • xmltocsharp转换器here
  

XSD => XML => C#类

XSD示例:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

转换为XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Technologies Online Tools 1.0 (https://www.liquid-technologies.com) -->
<shiporder xsi:noNamespaceSchemaLocation="schema.xsd" orderid="string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <orderperson>string</orderperson>
  <shipto>
    <name>string</name>
    <address>string</address>
    <city>string</city>
    <country>string</country>
  </shipto>
  <item>
    <title>string</title>
    <note>string</note>
    <quantity>3229484693</quantity>
    <price>-6894.465094196054907</price>
  </item>
  <item>
    <title>string</title>
    <note>string</note>
    <quantity>2181272155</quantity>
    <price>-2645.585094196054907</price>
  </item>
  <item>
    <title>string</title>
    <note>string</note>
    <quantity>2485046602</quantity>
    <price>4023.034905803945093</price>
  </item>
  <item>
    <title>string</title>
    <note>string</note>
    <quantity>1342091380</quantity>
    <price>-810.825094196054907</price>
  </item>
</shiporder>

将转换为此类结构:

   /* 
    Licensed under the Apache License, Version 2.0

    http://www.apache.org/licenses/LICENSE-2.0
    */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="shipto")]
    public class Shipto {
        [XmlElement(ElementName="name")]
        public string Name { get; set; }
        [XmlElement(ElementName="address")]
        public string Address { get; set; }
        [XmlElement(ElementName="city")]
        public string City { get; set; }
        [XmlElement(ElementName="country")]
        public string Country { get; set; }
    }

    [XmlRoot(ElementName="item")]
    public class Item {
        [XmlElement(ElementName="title")]
        public string Title { get; set; }
        [XmlElement(ElementName="note")]
        public string Note { get; set; }
        [XmlElement(ElementName="quantity")]
        public string Quantity { get; set; }
        [XmlElement(ElementName="price")]
        public string Price { get; set; }
    }

    [XmlRoot(ElementName="shiporder")]
    public class Shiporder {
        [XmlElement(ElementName="orderperson")]
        public string Orderperson { get; set; }
        [XmlElement(ElementName="shipto")]
        public Shipto Shipto { get; set; }
        [XmlElement(ElementName="item")]
        public List<Item> Item { get; set; }
        [XmlAttribute(AttributeName="noNamespaceSchemaLocation", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string NoNamespaceSchemaLocation { get; set; }
        [XmlAttribute(AttributeName="orderid")]
        public string Orderid { get; set; }
        [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
    }

}

注意!考虑到这只是“入门”,结果显然需要改进!

答案 7 :(得分:0)

我在Windows命令提示符中使用了xsd.exe

但是,由于我的xml引用了几个在线xml(在我的情况下http://www.w3.org/1999/xlink.xsd引用了http://www.w3.org/2001/xml.xsd),我还必须下载这些原理图,将它们放在与我的xsd相同的目录中,然后列出命令中的那些文件:

  

“C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v8.1A \ bin \ NETFX 4.5.1 Tools \ xsd.exe”/ classes / language:CS your.xsd xlink.xsd xml.xsd < / p>

答案 8 :(得分:0)

在我的情况下有效的命令是:

xsd /c your.xsd 

答案 9 :(得分:0)

与WSDL一起,我有xsd文件。以上在我的情况下没有工作给出了错误。它的工作方式如下

wsdl /l:C# /out:D:\FileName.cs D:\NameApi\wsdl_1_1\RESAdapterService.wsdl 
D:\CXTypes.xsd D:\CTypes.xsd 
D:\Preferences.xsd 

答案 10 :(得分:0)

如果要生成具有自动属性的类,请使用 this 将 XSD 转换为 XML,然后使用 this 将 XML 转换为 JSON 并将结果复制到剪贴板。 然后在 VS 中,在将创建类的文件中,转到“编辑”>“选择性粘贴”>“将 JSON 粘贴为类”。

相关问题