共用方式為


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>

適用於

另請參閱