XML Şemalarını Çapraz Geçirme
Şema Nesne Modeli (SOM) API'sini kullanarak XML şemasında geçiş yapmak, SOM'de depolanan öğelere, özniteliklere ve türlere erişim sağlar. SOM'a yüklenen bir XML şemasını geçirme, SOM API'sini kullanarak XML şemasını düzenlemenin de ilk adımıdır.
XML Şemasında Geçiş Yapma
Sınıfının aşağıdaki özellikleri XmlSchema , XML şemasına eklenen tüm genel öğelerin koleksiyonuna erişim sağlar.
Özellik | Koleksiyonda veya dizide depolanan nesne türü |
---|---|
Elements | XmlSchemaElement |
Attributes | XmlSchemaAttribute |
AttributeGroups | XmlSchemaAttributeGroup |
Groups | XmlSchemaGroup |
Includes | XmlSchemaExternal, XmlSchemaInclude, XmlSchemaImportveya XmlSchemaRedefine |
Items | XmlSchemaObject (tüm genel düzey öğelerine, özniteliklerine ve türlerine erişim sağlar). |
Notations | XmlSchemaNotation |
SchemaTypes | XmlSchemaType, XmlSchemaSimpleType, XmlSchemaComplexType |
UnhandledAttributes | XmlAttribute (şema ad alanına ait olmayan özniteliklere erişim sağlar) |
Not
Özellik dışında Items yukarıdaki tabloda listelenen tüm özellikler şema derlenene kadar kullanılamayan Şema Sonrası Derleme-Bilgi Kümesi (PSCI) özellikleridir. Items özelliği, şema derlenip tüm genel düzey öğelerine, özniteliklerine ve türlerine erişmek ve bunları düzenlemek için kullanılmadan önce kullanılabilecek bir şema öncesi derleme özelliğidir.
özelliği, UnhandledAttributes şema ad alanına ait olmayan tüm özniteliklere erişim sağlar. Bu öznitelikler şema işlemcisi tarafından işlenmez.
Aşağıdaki kod örneği, XML Şemaları Oluşturma konusunda oluşturulan müşteri şemasında dolaşmayı gösterir. Kod örneği, yukarıda açıklanan koleksiyonları kullanarak şemada dolaşmayı gösterir ve şemadaki tüm öğeleri ve öznitelikleri konsola yazar.
Örnek, aşağıdaki adımlarda müşteri şemasında geçiş yapar.
Müşteri şemasını yeni XmlSchemaSet bir nesneye ekler ve sonra derler. Şemayı okurken veya derlerken karşılaşılan tüm şema doğrulama uyarıları ve hataları temsilci tarafından ValidationEventHandler işlenir.
özelliği üzerinden Schemas yineleyerek öğesinden XmlSchemaSet derlenmiş XmlSchema nesneyi alır. Şema derlendiğinden Post-Schema-Compilation-Infoset (PSCI) özelliklerine erişilebilir.
Her öğenin adını konsola Values yazarak şema sonrası derleme XmlSchema.Elements koleksiyonunun koleksiyonundaki her öğeyi yinelerXmlSchemaElement.
sınıfını kullanarak XmlSchemaComplexType öğenin karmaşık türünü
Customer
alır.Karmaşık türün öznitelikleri varsa, her XmlSchemaAttribute biri üzerinde numaralandırmak için bir IDictionaryEnumerator alır ve konsola adını yazar.
sınıfını kullanarak karmaşık türün dizi parçacığını XmlSchemaSequence alır.
Konsola her XmlSchemaElement alt öğenin adını yazarak koleksiyondaki XmlSchemaSequence.Items her öğeyi yineler.
Aşağıda tam kod örneği verilmiştir.
#using <System.Xml.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Xml;
using namespace System::Xml::Schema;
ref class XmlSchemaTraverseExample
{
public:
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 = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew 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 = nullptr;
for each (XmlSchema^ schema in schemaSet->Schemas())
{
customerSchema = schema;
}
// Iterate over each XmlSchemaElement in the Values collection
// of the Elements property.
for each (XmlSchemaElement^ element in customerSchema->Elements->Values)
{
Console::WriteLine("Element: {0}", element->Name);
// Get the complex type of the Customer element.
XmlSchemaComplexType^ complexType = dynamic_cast<XmlSchemaComplexType^>(element->ElementSchemaType);
// 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 =
dynamic_cast<XmlSchemaAttribute^>(enumerator->Value);
Console::WriteLine("Attribute: {0}", attribute->Name);
}
}
// Get the sequence particle of the complex type.
XmlSchemaSequence^ sequence = dynamic_cast<XmlSchemaSequence^>(complexType->ContentTypeParticle);
// Iterate over each XmlSchemaElement in the Items collection.
for each (XmlSchemaElement^ childElement in sequence->Items)
{
Console::WriteLine("Element: {0}", 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);
}
};
int main()
{
XmlSchemaTraverseExample::Main();
return 0;
};
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: {0}", 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: {0}", 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: {0}", 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
XmlSchemaElement.ElementSchemaType özelliği olabilir XmlSchemaSimpleTypeveya XmlSchemaComplexType kullanıcı tanımlı basit bir tür veya karmaşık bir türse. Ayrıca, W3C XML Şema Önerisi'nde tanımlanan yerleşik veri türlerinden biriyse de olabilir XmlSchemaDatatype . Müşteri şemasında öğesinin öğesi, FirstName
XmlSchemaComplexTypeve LastName
öğeleri ise şeklindedirXmlSchemaSimpleType.ElementSchemaTypeCustomer
XML Şemaları Oluşturma konu başlığındaki kod örneği, özniteliğini CustomerId
öğesine eklemek için Customer
koleksiyonu kullandıXmlSchemaComplexType.Attributes. Bu bir şema öncesi derleme özelliğidir. Karşılık gelen Post-Schema-Compilation-Infoset özelliği XmlSchemaComplexType.AttributeUses , tür türetme yoluyla devralınanlar da dahil olmak üzere karmaşık türün tüm özniteliklerini tutan koleksiyondur.
Ayrıca bkz.
Geri Bildirim
https://aka.ms/ContentUserFeedback.
Çok yakında: 2024 boyunca, içerik için geri bildirim mekanizması olarak GitHub Sorunları’nı kullanımdan kaldıracak ve yeni bir geri bildirim sistemiyle değiştireceğiz. Daha fazla bilgi için bkz.Gönderin ve geri bildirimi görüntüleyin