如何使用Visual Studio 2010从xsd文件生成.NET 4.0 c#类(实体)有哪些选项?
很简单,只需在VS命令提示符下运行:
xsd your.xsd /classes
(这将创建 your.cs
)。但请注意,自2.0以来,这里的大多数内在选项并没有发生太多变化。
有关选项,请使用 xsd /?
或参见MSDN;例如,/enableDataBinding
可能很有用。
xsd schema1.xsd schema2.xsd schema3.xsd /c
- mivra为了获得一个快速且简便的解决方案(而且不需要使用VS),可以尝试这些在线转换工具:
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; }
}
}
注意!请注意这仅是为了帮助您入门,结果显然需要改进!
xsd.exe在存在循环引用时表现不佳(即类型可以直接或间接拥有自己类型的元素)。
当存在循环引用时,我使用Xsd2Code。 Xsd2Code能够很好地处理循环引用,并在VS IDE内工作,这是一个很大的优势。 它还具有许多可用的功能,例如生成序列化/反序列化代码。 但是,请确保在生成序列化时打开GenerateXMLAttributes(否则,如果未在所有元素上定义,则会出现排列异常)。
两者都无法很好地处理选择功能。您将得到对象的列表/集合而不是想要的类型。 如果可能的话,建议避免在xsd中使用choice,因为它不能很好地序列化/反序列化为强类型类。 不过,如果您不介意,那么这不是问题。
xsd2code中的任何功能反序列化为System.Xml.XmlElement,我发现这非常方便,但如果您想要强类型对象,则可能会成为问题。 我经常在允许自定义配置数据时使用any,因此XmlElement方便传递到其他自定义定义的XML反序列化程序。
我在批处理脚本中使用 XSD
来直接从 XML
生成 .xsd
文件和类:
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%
xsd /c your.xsd
我在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
xsd.exe
时遇到了很多问题,因为它不支持循环引用,但最终我还是成功了。 - Aske B.- The table (Amt) cannot be the child table to itself in nested relations.
根据KB325695的说明,我将扩展名从.xml改为.xsd,然后它就正常工作了。