我的XSD如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Test"
targetNamespace="http://tempuri.org/Test.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Test.xsd"
xmlns:mstns="http://tempuri.org/Test.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="TestCurrency">
<xs:sequence>
<xs:element name="CurrencyRef" minOccurs="1" maxOccurs="1" type="xs:int"/>
<xs:element name="CurrentRefSpecified" minOccurs="1" maxOccurs="1" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Currency" type="TestCurrency"/>
</xs:schema>
在VS 2008命令提示符下使用以下命令运行XSD Tool时: xsd / classes / language:vb test.xsd
我得到以下代码
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.3615
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=2.0.50727.3038.
'
'''<remarks/>
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://tempuri.org/Test.xsd"), _
System.Xml.Serialization.XmlRootAttribute("Currency", [Namespace]:="http://tempuri.org/Test.xsd", IsNullable:=false)> _
Partial Public Class TestCurrency
Private currencyRefField As Integer
Private currentRefSpecified1Field As Integer
'''<remarks/>
Public Property CurrencyRef() As Integer
Get
Return Me.currencyRefField
End Get
Set
Me.currencyRefField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("CurrentRefSpecified")> _
Public Property CurrentRefSpecified1() As Integer
Get
Return Me.currentRefSpecified1Field
End Get
Set
Me.currentRefSpecified1Field = value
End Set
End Property
End Class
当我指定的元素是CurrentRefSpecified时。为什么它在生成的类中显示为CurrentRefSpecified1?这是XSD问题还是我的问题?
任何人都可以快速回答这个问题吗?
当我们有元素名称'xyzspecified'时,会自动发生这种追加..类文件中的属性设置为xyzspecified1 ...
答案 0 :(得分:1)
我不确定,但这是我的假设:对于XML中的某些字段,如果它们可以省略或指定,xsd.exe
将生成一个名为(yourfieldname)Specified
的相应字段来发出信号是否实际指定了可能的可终止/缺失值。
现在,您的示例中的情况并非如此,但如果您要更改XSD以使您的字段CurrentRef
可以为空,或者minOccurs=0
,则xsd。 exe工具必须为它创建一个CurrentRefSpecified
辅助字段,这将与您在XSD中已存在的字段冲突。
我猜xsd工具会将您的字段CurrentRefSpecified
“重命名”为CurrentRefSpecified1
,以避免任何潜在的(未来)冲突。