简体   繁体   English

如何使用XSD.exe从C#类型生成XML架构,以便将[XmlAttribute]属性映射到所需的XML属性?

[英]How to generate XML Schema from C# type with XSD.exe such that [XmlAttribute] property is mapped to required XML attribute?

Put it simply, when I use XSD.exe (that comes with Visual Studio 2012) to generate XML schema file from this class: 简单地说,当我使用XSD.exe(Visual Studio 2012附带)从此类生成XML模式文件时:

[Serializable]
public class Person
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public int Age { get; set; }
}

I get this as the result: 我得到的结果是:

<?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>

Notice that Age attribute is specified as required (it has use="required" ) in generated schema while attribute Name is not. 请注意,在生成的模式中将Age属性指定为必需属性( 使用use =“ required” ),而未指定Name属性。

I use XSD.exe like this: 我这样使用XSD.exe:

xsd.exe Sample.exe /type:Person

Where Sample.exe is .NET assembly where Person class is defined. 其中Sample.exe是.NET程序集,其中定义了Person类。

I would like to somehow specify in my class which XmlAttribute properties are required and which are not so that XSD.exe can automatically generate schema from that. 我想以某种方式在类中指定哪些XmlAttribute属性是必需的,哪些不是必需的,以便XSD.exe可以从中自动生成架构。 Is this possible? 这可能吗?

Unless there's a bug in XSD (it is not clear if you tried what it is described in the XSD.exe documentation, specifically the attribute element binding support - right now I can't test it), the answer is yes, you can. 除非XSD中存在错误(不清楚您是否尝试过XSD.exe文档中描述的内容,特别是属性元素绑定支持 -现在我无法对其进行测试),否则答案是肯定的,可以。

In your case, the different behaviour between Name and Age is simply due to the fact that a String field is nullable, whereas the int one is not (somehow I don't believe an int? will make a difference in your case, still you can try it...) Attributes are not nillable (from an XSD perspective), therefore the use of optional. 在您的情况下,“名称”和“年龄”之间的不同行为仅是由于以下事实:“字符串”字段可为空,而“整数”字段不是(某种程度上,我不相信一个“ int?会在您的情况下有所作为,但仍然如此)可以尝试...)属性不是不可设置的(从XSD角度来看),因此使用可选属性。

Use Attribute: Generating an XML Schema document from classes 使用属性:从类生成XML模式文档

In either of the following two cases, Xsd.exe does not specify the use attribute, reverting to the default value optional: 在以下两种情况中的任何一种情况下,Xsd.exe均未指定use属性,而是恢复为默认值optional:

• An extra public bool field that follows the Specified naming convention is present. •存在遵循指定命名约定的额外公共布尔字段。

• A default value is assigned to the member via an attribute of type System.Component.DefaultValueAttribute. •通过类型System.Component.DefaultValueAttribute的属性将默认值分配给成员。

If neither of these conditions is met, Xsd.exe produces a value of required for the use attribute. 如果这两个条件都不满足,则Xsd.exe会为use属性生成required值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM