微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从类创建XSD架构?

如何解决如何从类创建XSD架构?

您可以使用XSD.exe(可从Visual Studio安装中获得。)

public sealed class Configuration
{
 public string Name { get; set; }
 public Levels Level { get; set; }
 public ConfigurationSpec Spec { get; set; }
}
 public abstract class ConfigurationSpec { }
 public class ConfigurationSpec1    {   }
public class ConfigurationSpec2 {   }

结果是

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Levels" type="Levels" />
  <xs:simpleType name="Levels">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Easy" />
      <xs:enumeration value="Medium" />
      <xs:enumeration value="Hard" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="Configuration" nillable="true" type="Configuration" />
  <xs:complexType name="Configuration">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
      <xs:element minOccurs="1" maxOccurs="1" name="Level" type="Levels" />
      <xs:element minOccurs="0" maxOccurs="1" name="Spec" type="ConfigurationSpec" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ConfigurationSpec" abstract="true" />
  <xs:element name="ConfigurationSpec" nillable="true" type="ConfigurationSpec" />
  <xs:element name="ConfigurationSpec1" nillable="true" type="ConfigurationSpec1" />
  <xs:complexType name="ConfigurationSpec1" />
  <xs:element name="ConfigurationSpec2" nillable="true" type="ConfigurationSpec2" />
  <xs:complexType name="ConfigurationSpec2" />
</xs:schema>

要做的就是编译程序集,并XSD.exe以程序集的路径作为参数运行。XSD.exe /?也具有所有参数的列表。

例: XSD.exe C:\Dev\Project1\Bin\Debug\library.dll

解决方法

我在使用XSD文件时遇到了麻烦。

我试图从一个类创建一个XSD文件:

public enum Levels { Easy,Medium,Hard }
public sealed class Configuration
{
    public string Name { get;set; }
    public Levels Level { get; set; }
    public ConfigurationSpec { get;set;}
}

public abstract class ConfigurationSpec { }
public class ConfigurationSpec1
{
    // ...
}
public class ConfigurationSpec2
{
    // ...
}

请注意,我在Configuration中有一个抽象类。有了该功能,是否可以创建XSD?如果可以,如何创建?

想法是将类Configuration传递给XSD。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。