XmlSchemaValidator.GetExpectedParticles 方法

定义

返回当前元素上下文中的期望微粒。

public:
 cli::array <System::Xml::Schema::XmlSchemaParticle ^> ^ GetExpectedParticles();
public System.Xml.Schema.XmlSchemaParticle[] GetExpectedParticles ();
member this.GetExpectedParticles : unit -> System.Xml.Schema.XmlSchemaParticle[]
Public Function GetExpectedParticles () As XmlSchemaParticle()

返回

如果没有期望粒子,则为一个 XmlSchemaParticle 对象数组或一个空数组。

注解

GetExpectedParticles 方法可以返回的有效粒子是 XmlSchemaElementXmlSchemaAny 类的实例。

如果内容模型的复合器是 xs:sequence,则只返回序列中的下一个粒子。 如果内容模型的复合器是 xs:allxs:choice,则返回当前元素上下文中可能出现的所有有效粒子。

例如,在下面的 XML 架构定义语言 (XSD) 架构和 XML 文档中,在验证 book 元素之后,book 元素是当前的元素上下文。 GetExpectedParticles 方法返回一个数组,包含表示 XmlSchemaElement 元素的单个 title 对象。 如果验证上下文是 title 元素,GetExpectedParticles 方法将返回一个空数组。 如果 GetExpectedParticles 方法在验证了 title 元素之后但是在验证 description 元素之前调用,将返回一个数组,包含表示 XmlSchemaElement 元素的单个 description 对象。 如果 GetExpectedParticles 方法在验证了 description 元素之后调用,将返回一个数组,包含表示通配符的单个 XmlSchemaAny 对象。

Dim reader As XmlReader =  XmlReader.Create("input.xml")   

Dim schemaSet As XmlSchemaSet =  New XmlSchemaSet()   
schemaSet.Add(Nothing, "schema.xsd")  
Dim manager As XmlNamespaceManager =  New XmlNamespaceManager(reader.NameTable)   

Dim validator As XmlSchemaValidator =  New XmlSchemaValidator(reader.NameTable,schemaSet,manager,XmlSchemaValidationFlags.None)  
validator.Initialize()  

validator.ValidateElement("book", "", Nothing)  
validator.GetUnspecifiedDefaultAttributes(New ArrayList())  
validator.ValidateEndOfAttributes(Nothing)  
For Each element As XmlSchemaElement In validator.GetExpectedParticles()  
    Console.WriteLine(element.Name)  
Next  

validator.ValidateElement("title", "", Nothing)  
validator.GetUnspecifiedDefaultAttributes(New ArrayList())  
validator.ValidateEndOfAttributes(Nothing)  
For Each element As XmlSchemaElement In validator.GetExpectedParticles()  
    Console.WriteLine(element.Name)  
Next  
validator.ValidateEndElement(Nothing)  

For Each element As XmlSchemaElement In validator.GetExpectedParticles()  
    Console.WriteLine(element.Name)  
Next  

validator.ValidateElement("description", "", Nothing)  
validator.GetUnspecifiedDefaultAttributes(New ArrayList())  
validator.ValidateEndOfAttributes(Nothing)  
validator.ValidateEndElement(Nothing)  

For Each particle As XmlSchemaParticle In validator.GetExpectedParticles()  
    Console.WriteLine(particle.GetType())  
Next  

validator.ValidateElement("namespace", "", Nothing)  
validator.GetUnspecifiedDefaultAttributes(New ArrayList())  
validator.ValidateEndOfAttributes(Nothing)  
validator.ValidateEndElement(Nothing)  

validator.ValidateEndElement(Nothing)  
XmlReader reader = XmlReader.Create("input.xml");  

XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add(null, "schema.xsd");  
XmlNamespaceManager manager = new XmlNamespaceManager(reader.NameTable);  

XmlSchemaValidator validator = new XmlSchemaValidator(reader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None);  
validator.Initialize();  

validator.ValidateElement("book", "", null);  

validator.GetUnspecifiedDefaultAttributes(new ArrayList());  
validator.ValidateEndOfAttributes(null);  
foreach (XmlSchemaElement element in validator.GetExpectedParticles())  
{  
    Console.WriteLine(element.Name);  
}  

validator.ValidateElement("title", "", null);  
validator.GetUnspecifiedDefaultAttributes(new ArrayList());  
validator.ValidateEndOfAttributes(null);  
foreach (XmlSchemaElement element in validator.GetExpectedParticles())  
{  
    Console.WriteLine(element.Name);  
}  
validator.ValidateEndElement(null);  

foreach (XmlSchemaElement element in validator.GetExpectedParticles())  
{  
    Console.WriteLine(element.Name);  
}  

validator.ValidateElement("description", "", null);  
validator.GetUnspecifiedDefaultAttributes(new ArrayList());  
validator.ValidateEndOfAttributes(null);  
validator.ValidateEndElement(null);  

foreach (XmlSchemaParticle particle in validator.GetExpectedParticles())  
{  
    Console.WriteLine(particle.GetType());  
}  

validator.ValidateElement("namespace", "", null);  
validator.GetUnspecifiedDefaultAttributes(new ArrayList());  
validator.ValidateEndOfAttributes(null);  
validator.ValidateEndElement(null);  

validator.ValidateEndElement(null);  

该示例使用以下 XML 作为输入。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="book">

<xs:complexType>

<xs:sequence>

<xs:element name="title" type="xs:string" />

<xs:element name="description" type="xs:string" />

<xs:any processContents ="lax"/>

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:schema>

该示例使用以下 XSD 架构作为输入。

<book>

<title>My Book</title>

<description>My Book's Description</description>

<namespace>System.Xml.Schema</namespace>

</book>

适用于

另请参阅