This topic has not yet been rated - Rate this topic

XmlSchemaValidator.GetExpectedParticles Method

Returns the expected particles in the current element context.

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)
public XmlSchemaParticle[] GetExpectedParticles()

Return Value

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

The valid particles that can be returned by the GetExpectedParticles method are instances of the XmlSchemaElement and 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 GetExpectedParticles method returns an array containing a single XmlSchemaElement object representing the title element. When the validation context is the title element, the GetExpectedParticles method returns an empty array. If the 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 XmlSchemaElement object representing the description element. If the GetExpectedParticles method is called after the description element has been validated then it returns an array containing a single XmlSchemaAny object representing the wildcard.

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>

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
The example takes the following xxx as input
In the part where it describes the XSD and XML as input  it has the XSD and XML reversed.  It has The example takes the following XML as input.  But instead of the XML instance it has the XSD definition.  In The example takes the following XSD schema as input it lists the XML instance and not the XSD.  Easy to figure out but still wrong.