如何从C#类型的XML Schema与XSD.EXE这样[XmlAttribute]属性映射到所需的XML属性? [英] How to generate XML Schema from C# type with XSD.exe such that [XmlAttribute] property is mapped to required XML attribute?

查看:164
本文介绍了如何从C#类型的XML Schema与XSD.EXE这样[XmlAttribute]属性映射到所需的XML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,当我使用XSD.EXE(随Visual Studio 2012中)来生成从这个类XML schema文件:



<预类=郎-CS prettyprint-覆盖> [Serializable接口]
公共类Person
{
[XmlAttribute]
公共字符串名称{;组; }

[XmlAttribute]
公众诠释年龄{搞定;组; }
}



我得到这个作为结果:



<预类=郎咸平的XML prettyprint-覆盖> <?XML版本=1.0编码=UTF-8>?;
< XS:架构将elementFormDefault =合格的xmlns:XS =http://www.w3.org/2001/XMLSchema>
< XS:元素的名称=人的nillable =真类型=人/>
< XS:复杂类型名称=人>
< XS:属性名称=名称TYPE =XS:字符串/>
< XS:属性名称=时代类型=XS:INT使用=需要/>
< / XS:复杂类型>
< / XS:架构>

这是年龄的作为需要指定的属性

的通知(它的 。使用=需要在)生成的模式,而属性的名称的不



我用XSD.EXE这样的:

  XSD.EXE SAMPLE.EXE /类型:个人

在哪里SAMPLE.EXE就是Person类定义.NET程序集。



我想在我的类XmlAttribute属性是必需的,这在某种程度上指明不是这样XSD.EXE可以自动生成从架构。这可能吗?


解决方案

除非有一个在XSD(一个bug,目前尚不清楚,如果你尝试过什么的XSD描述.EXE的文件,特别是属性元件结合支持 - 我现在不能测试),答案是肯定的,你可以。



在你的情况,姓名和年龄之间的不同的行为简单地归结于一个事实,即一个字符串字段为空,而INT一个不是(不知何故我不难以相信,一个 INT?将在你的情况有差别,还是可以尝试一下......)属性不是的nillable(从XSD的角度),因此使用。可选的




使用属性:生成的类中的XML Schema文档。



在无论是以下两种情况下,XSD.EXE不指定使用
属性,恢复到默认值可选:



•一个额外的公共BOOL字段遵循指定的命名规则存在。



•默认值是通过类型
System.Component.DefaultValueAttribute的属性分配给成员。



如果这两个条件都不满足,产生XSD.EXE为使用所需的属性值。



Put it simply, when I use XSD.exe (that comes with Visual Studio 2012) to generate XML schema file from this class:

[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.

I use XSD.exe like this:

xsd.exe Sample.exe /type:Person

Where Sample.exe is .NET assembly where Person class is defined.

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

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.

Use Attribute: Generating an XML Schema document from classes

In either of the following two cases, Xsd.exe does not specify the use attribute, reverting to the default value 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.

If neither of these conditions is met, Xsd.exe produces a value of required for the use attribute.

这篇关于如何从C#类型的XML Schema与XSD.EXE这样[XmlAttribute]属性映射到所需的XML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆