/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/VS_Snippets_Data/XmlSchemaTraverseExample/CS/XmlSchemaTraverseExample.cs
74 строки
3 KB
Next Turn
Normalizes trailing spaces in samples, Part 2 (#17933)
20 апр 2020, 17:27
Не верифицирован
20 апр 2020, 17:27
3d85632
Код
Авторство
О чём код?
//<snippet1> 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); } } //</snippet1>