我正在针对这个 XSD 验证一些 XML:
<xs:element name="Composite">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="HeldCurrency" type="mstns:HeldCurrencyType" nillable="true"/>
<xs:element name="Component" type="mstns:ComponentType" nillable="true" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="Type" type="mstns:compositeType" use="required"/>
</xs:complexType>x
</xs:element>
<xs:complexType name="HeldCurrencyType">
<xs:attribute name="CashPerCreationUnit" type="xs:double" />
<xs:attribute name="CashCurrency" type="xs:string" />
<xs:attribute name="ClosingSpotFx" type="xs:double" />
</xs:complexType>
<xs:complexType name="ComponentType">
<xs:choice>
<xs:element name="Weight" type="xs:double"/>
<xs:element name="Units" type="xs:int"/>
</xs:choice>
<xs:attribute name="Symbol" type="xs:string" />
</xs:complexType>
<xs:simpleType name="compositeType">
<xs:restriction base="xs:string">
<xs:enumeration value="Index" />
<xs:enumeration value="Etf" />
</xs:restriction>
</xs:simpleType>
当我对它运行以下 XML 时:
<?xml version="1.0"?>
<Composite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Type="Etf" xmlns="urn:xxx-au:index-types">
<HeldCurrency />
<Component />
</Composite>
我使用以下代码在 C#/.Net4.0 中运行它:
using (FileStream fileStream = File.OpenRead("./Xml/Components.xsd"))
{
using (var schemaReader = new XmlTextReader(fileStream))
{
settings.Schemas.Add(null, schemaReader);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += OnValidationEventHandler;
using (FileStream infile = File.OpenRead(filename))
{
using(var xmlReader = XmlReader.Create(infile,settings))
{
var serialiser = new XmlSerializer(typeof (Composite));
_composite = (Composite) serialiser.Deserialize(xmlReader);
}
}
}
}
我收到以下错误:命名空间“urn:xxx-au:index-types”中的元素“组件”内容不完整。预期的可能元素列表:命名空间中的“重量,单位”。但是,我有 minOccurs="0" 那么为什么这会是一个问题呢?毕竟,序列不包含 ComponentType 元素是合法的,对吧?
提前致谢。