Mar*_*ell 471
很简单; 刚运行(在vs命令提示符下)
xsd your.xsd /classes
Run Code Online (Sandbox Code Playgroud)
(将创建your.cs).但请注意,自2.0以来,此处的大多数内在选项都没有太大变化
有关选项,请使用xsd /?或查看MSDN ; 例如/enableDataBinding可能有用.
Vot*_*fee 14
当你有循环引用时,xsd.exe不能正常工作(即一个类型可以直接或间接拥有自己类型的元素).
当存在循环引用时,我使用Xsd2Code.Xsd2Code很好地处理循环引用,并在VS IDE中工作,这是一个很大的优点.它还具有许多功能,例如生成序列化/反序列化代码.如果您正在生成序列化,请确保打开GenerateXMLAttributes(否则如果未在所有元素上定义,您将获得排序异常).
两者都不适用于选择功能.你最终会得到对象的列表/集合而不是你想要的类型.如果可能的话,我建议在xsd中避免选择,因为这不会很好地序列化/反序列化为强类型类.如果你不关心这个,那么这不是问题.
xsd2code中的任何功能反序列化为System.Xml.XmlElement,我觉得这很方便但如果你想要强类型对象可能会有问题.我经常在允许自定义配置数据时使用any,因此XmlElement可以方便地传递给另一个在其他地方自定义的XML反序列化器.
我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%
Run Code Online (Sandbox Code Playgroud)
对于快速懒惰的解决方案,(并且根本不使用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>
Run Code Online (Sandbox Code Playgroud)
转换为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>
Run Code Online (Sandbox Code Playgroud)
转换为此类结构:
/*
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
注意!考虑到这只是“入门”,结果显然需要改进!
我向您展示了使用Vs2017和Vs2019的最简单方法,使用Visual Studio打开xsd并生成一个示例xml文件,如建议的url中所示。
2.在“ XML Schema Explorer”中,一直向下滚动以找到根/数据节点。右键单击根/数据节点,它将显示“ Generate Sample XML”。如果未显示,则表示您不在数据元素节点上,但在任何数据定义节点上。