【发布时间】:2014-01-06 19:51:45
【问题描述】:
简单地说,当我使用 XSD.exe(Visual Studio 2012 附带的)从这个类生成 XML 模式文件时:
[Serializable]
public class Person
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public int Age { get; set; }
}
结果如下:
<?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:attribute name="Name" type="xs:string" />
<xs:attribute name="Age" type="xs:int" use="required" />
</xs:complexType>
</xs:schema>
注意 Age 属性在生成的架构中被指定为必需(它具有 use="required"),而属性 Name 不是。
我像这样使用 XSD.exe:
xsd.exe Sample.exe /type:Person
Sample.exe 是定义 Person 类的 .NET 程序集。
我想以某种方式在我的类中指定哪些 XmlAttribute 属性是必需的,哪些不是,以便 XSD.exe 可以从中自动生成架构。这可能吗?
【问题讨论】:
-
FWIW,XSD.EXE 不是一个好工具。微软几年前就停止了它的工作,我仍然发现它偶尔会出现错误。
-
您能推荐一个替代工具吗?每当我在 C# 代码中更改我的 XML-serializable 类时,我想自动生成 XSD 文件(在项目构建后事件中)。
-
XmlSpy 很不错。如果你想要一个免费的工具,你可以试试这个:codeproject.com/Articles/133570/…
-
感谢您的建议。实际上,我需要从 C# 代码(或更一般地说,从 .NET 类型/类)生成 XSD 文件。使用指定的工具可以做到这一点吗?
-
我刚刚检查过,文章是关于从 XML 而非 C# 类生成 XSD。这并不能解决我的问题,但感谢您的努力,我很感激。
标签: c# xml xsd xml-serialization