System.Xml.Schema.XmlSchemaValidator.GetExpectedParticles Method

Returns the expected particles in the current element context.

Syntax

public XmlSchemaParticle[] GetExpectedParticles ()

Returns

An array of System.Xml.Schema.XmlSchemaParticle objects or an empty array if there are no expected particles.

Remarks

The valid particles that can be returned by the XmlSchemaValidator.GetExpectedParticles method are instances of the System.Xml.Schema.XmlSchemaElement and System.Xml.Schema.XmlSchemaAny classes.

When the compositor for the content model is an xs:sequence, only the next particle in the sequence is returned. If the compositor for the content model is an xs:all or an xs:choice, then all valid particles that could follow in the current element context are returned.

For example, in the XML Schema Definition Language (XSD) schema and XML document that follow, after validating the book element, the book element is the current element context. The XmlSchemaValidator.GetExpectedParticles method returns an array containing a single System.Xml.Schema.XmlSchemaElement object representing the title element. When the validation context is the title element, the XmlSchemaValidator.GetExpectedParticles method returns an empty array. If the XmlSchemaValidator.GetExpectedParticles method is called after the title element has been validated but before the description element has been validated, it returns an array containing a single System.Xml.Schema.XmlSchemaElement object representing the description element. If the XmlSchemaValidator.GetExpectedParticles method is called after the description element has been validated then it returns an array containing a single System.Xml.Schema.XmlSchemaAny object representing the wildcard.

Example

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)

Example

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);

The example takes the following XML as input.

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

The example takes the following XSD schema as input.

<book>

<title>My Book</title>

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

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

</book>

Requirements

Namespace: System.Xml.Schema
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0