Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Menelusuri skema XML menggunakan Schema Object Model (SOM) API menyediakan akses ke elemen, atribut, dan jenis yang disimpan di SOM. Menelusuri skema XML yang dimuat ke dalam SOM juga merupakan langkah pertama dalam mengedit skema XML menggunakan SOM API.
Menelusuri Skema XML
Properti XmlSchema kelas berikut menyediakan akses ke kumpulan semua item global yang ditambahkan ke skema XML.
| Harta benda | Jenis objek yang disimpan dalam koleksi atau array |
|---|---|
| Elements | XmlSchemaElement |
| Attributes | XmlSchemaAttribute |
| AttributeGroups | XmlSchemaAttributeGroup |
| Groups | XmlSchemaGroup |
| Includes | XmlSchemaExternal, XmlSchemaInclude, XmlSchemaImport, atau XmlSchemaRedefine |
| Items | XmlSchemaObject (menyediakan akses ke semua elemen, atribut, dan jenis tingkat global). |
| Notations | XmlSchemaNotation |
| SchemaTypes | XmlSchemaType, , XmlSchemaSimpleTypeXmlSchemaComplexType |
| UnhandledAttributes | XmlAttribute (menyediakan akses ke atribut yang bukan milik namespace skema) |
Nota
Semua properti yang tercantum dalam tabel di atas, kecuali properti Items, merupakan properti Post-Schema-Compilation-Infoset (PSCI) yang tidak tersedia hingga skema dikompilasi. Properti Items adalah properti pra-kompilasi skema yang dapat digunakan sebelum skema dikompilasi untuk mengakses dan mengedit semua elemen, atribut, dan jenis tingkat global.
Properti UnhandledAttributes menyediakan akses ke semua atribut yang bukan milik ruang nama skema. Atribut ini tidak diproses oleh prosesor skema.
Contoh kode berikut menunjukkan cara menelusuri skema pelanggan yang dibuat dalam topik Membangun Skema XML. Contoh kode menunjukkan melintas skema menggunakan koleksi yang dijelaskan di atas dan menulis semua elemen dan atribut dalam skema ke konsol.
Sampel melintasi skema pelanggan dalam langkah-langkah berikut.
Menambahkan skema pelanggan ke objek baru XmlSchemaSet lalu mengkompilasinya. Setiap peringatan validasi skema dan kesalahan yang ditemui saat membaca atau mengkompilasi skema ditangani oleh ValidationEventHandler delegasi.
Mengambil objek yang sudah dikompilasi XmlSchema dari XmlSchemaSet dengan melakukan iterasi pada properti Schemas. Karena skema dikompilasi, properti Post-Schema-Compilation-Infoset (PSCI) dapat diakses.
Melakukan iterasi pada setiap XmlSchemaElement dalam koleksi Values setelah skema kompilasi XmlSchema.Elements yang menulis nama setiap elemen ke konsol.
Mendapatkan elemen
Customeryang bertipe kompleks menggunakan kelas XmlSchemaComplexType.Jika jenis kompleks memiliki atribut apa pun, dapatkan IDictionaryEnumerator untuk menelusuri setiap XmlSchemaAttribute dan menulis namanya ke layar konsol.
Mendapatkan partikel urutan dari jenis kompleks menggunakan kelas XmlSchemaSequence.
Melakukan iterasi pada masing-masing XmlSchemaElement dalam XmlSchemaSequence.Items koleksi yang menulis nama setiap elemen anak ke konsol.
Berikut ini adalah contoh kode lengkap.
using System;
using System.Collections;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaTraverseExample
{
static void Main()
{
// Add the customer schema to a new XmlSchemaSet and compile it.
// Any schema validation warnings and errors encountered reading or
// compiling the schema are handled by the ValidationEventHandler delegate.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.tempuri.org", "customer.xsd");
schemaSet.Compile();
// Retrieve the compiled XmlSchema object from the XmlSchemaSet
// by iterating over the Schemas property.
XmlSchema customerSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
customerSchema = schema;
}
// Iterate over each XmlSchemaElement in the Values collection
// of the Elements property.
foreach (XmlSchemaElement element in customerSchema.Elements.Values)
{
Console.WriteLine($"Element: {element.Name}");
// Get the complex type of the Customer element.
XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType;
// If the complex type has any attributes, get an enumerator
// and write each attribute name to the console.
if (complexType.AttributeUses.Count > 0)
{
IDictionaryEnumerator enumerator =
complexType.AttributeUses.GetEnumerator();
while (enumerator.MoveNext())
{
XmlSchemaAttribute attribute =
(XmlSchemaAttribute)enumerator.Value;
Console.WriteLine($"Attribute: {attribute.Name}");
}
}
// Get the sequence particle of the complex type.
XmlSchemaSequence sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
// Iterate over each XmlSchemaElement in the Items collection.
foreach (XmlSchemaElement childElement in sequence.Items)
{
Console.WriteLine($"Element: {childElement.Name}");
}
}
}
static void ValidationCallback(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.Write("WARNING: ");
else if (args.Severity == XmlSeverityType.Error)
Console.Write("ERROR: ");
Console.WriteLine(args.Message);
}
}
Imports System.Collections
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaTraverseExample
Shared Sub Main()
' Add the customer schema to a new XmlSchemaSet and compile it.
' Any schema validation warnings and errors encountered reading or
' compiling the schema are handled by the ValidationEventHandler delegate.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add("http://www.tempuri.org", "customer.xsd")
schemaSet.Compile()
' Retrieve the compiled XmlSchema object from the XmlSchemaSet
' by iterating over the Schemas property.
Dim customerSchema As XmlSchema = Nothing
For Each schema As XmlSchema In schemaSet.Schemas()
customerSchema = schema
Next
' Iterate over each XmlSchemaElement in the Values collection
' of the Elements property.
For Each element As XmlSchemaElement In customerSchema.Elements.Values
Console.WriteLine("Element: {0}", element.Name)
' Get the complex type of the Customer element.
Dim complexType As XmlSchemaComplexType = CType(element.ElementSchemaType, XmlSchemaComplexType)
' If the complex type has any attributes, get an enumerator
' and write each attribute name to the console.
If complexType.AttributeUses.Count > 0 Then
Dim enumerator As IDictionaryEnumerator = _
complexType.AttributeUses.GetEnumerator()
While enumerator.MoveNext()
Dim attribute As XmlSchemaAttribute = _
CType(enumerator.Value, XmlSchemaAttribute)
Console.WriteLine("Attribute: {0}", Attribute.Name)
End While
End If
' Get the sequence particle of the complex type.
Dim sequence As XmlSchemaSequence = CType(complexType.ContentTypeParticle, XmlSchemaSequence)
For Each childElement As XmlSchemaElement In sequence.Items
Console.WriteLine("Element: {0}", childElement.Name)
Next
Next
End Sub
Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Else
If args.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
End If
End If
Console.WriteLine(args.Message)
End Sub
End Class
Properti XmlSchemaElement.ElementSchemaType dapat berupa XmlSchemaSimpleType, atau XmlSchemaComplexType jika itu adalah jenis sederhana yang ditentukan pengguna atau jenis kompleks. Ini juga bisa berupa XmlSchemaDatatype jika itu adalah salah satu jenis data bawaan yang ditentukan dalam Rekomendasi Skema XML W3C. Dalam skema pelanggan, elemen ElementSchemaType adalah Customer, dan elemen XmlSchemaComplexType serta elemen FirstName adalah LastName.
Contoh kode dalam topik Membangun Skema XML menggunakan XmlSchemaComplexType.Attributes koleksi untuk menambahkan atribut CustomerId ke Customer elemen . Ini adalah properti sebelum kompilasi skema. Properti Post-Schema-Compilation-Infoset yang sesuai adalah koleksi XmlSchemaComplexType.AttributeUses (collection), yang menyimpan semua atribut dari jenis kompleks, termasuk semua yang diwarisi melalui turunan jenis.