XmlSchemaValidator.ValidateEndElement Yöntem

Tanım

Öğenin metin içeriğinin basit içeriğe sahip öğeler için veri türüne göre geçerli olup olmadığını doğrular ve geçerli öğenin içeriğinin karmaşık içeriğe sahip öğeler için eksiksiz olup olmadığını doğrular.

Aşırı Yüklemeler

ValidateEndElement(XmlSchemaInfo)

Öğenin metin içeriğinin basit içeriğe sahip öğeler için veri türüne göre geçerli olup olmadığını doğrular ve geçerli öğenin içeriğinin karmaşık içeriğe sahip öğeler için eksiksiz olup olmadığını doğrular.

ValidateEndElement(XmlSchemaInfo, Object)

Belirtilen öğenin metin içeriğinin veri türüne göre geçerli olup olmadığını doğrular.

ValidateEndElement(XmlSchemaInfo)

Kaynak:
XmlSchemaValidator.cs
Kaynak:
XmlSchemaValidator.cs
Kaynak:
XmlSchemaValidator.cs

Öğenin metin içeriğinin basit içeriğe sahip öğeler için veri türüne göre geçerli olup olmadığını doğrular ve geçerli öğenin içeriğinin karmaşık içeriğe sahip öğeler için eksiksiz olup olmadığını doğrular.

C#
public object? ValidateEndElement (System.Xml.Schema.XmlSchemaInfo? schemaInfo);
C#
public object ValidateEndElement (System.Xml.Schema.XmlSchemaInfo schemaInfo);

Parametreler

schemaInfo
XmlSchemaInfo

XmlSchemaInfo Öğesinin başarılı bir şekilde doğrulanmasıyla özellikleri ayarlanan bir nesne. Bu parametre olabilir null.

Döndürülenler

Öğenin basit içeriği varsa öğenin ayrıştırılmış, yazılan metin değeri.

Özel durumlar

Öğenin içeriği geçerli değil.

Yöntemi ValidateEndElement doğru sırada çağrılmıyor. Örneğin, yöntemi çağrıldıktan SkipToEndElement(XmlSchemaInfo)sonra çağrılırsaValidateEndElement.

Açıklamalar

Yöntemi kullanılırken ValidateEndElement dikkate alınması gereken önemli notlar aşağıdadır.

  • Basit içeriğe ValidateEndElement sahip öğeler için yöntemi, öğenin metin içeriğinin veri türüne göre geçerli olduğunu doğrular ve ayrıştırılmış türündeki değeri döndürür. öğesinin metin içeriği, öğesine yapılan önceki çağrıların ValidateTextsonuçlarından elde edilir.

  • Karmaşık içeriğe ValidateEndElement sahip öğeler için yöntemi, geçerli öğenin içeriğinin tamam olup olmadığını doğrular.

  • Öğenin varsayılan değeri varsa, yönteminden ValidateEndElement döndürülen değer öğenin varsayılan değeridir.

  • XmlSchemaValidator nesnesi değeriyle ProcessIdentityConstraints parametre olarak oluşturulduysa, ValidateEndElement yöntemi öğesi için geçerli olan kimlik kısıtlama kurallarını (varsa) denetler.

  • Öğesi geçerli bağlamda başarıyla doğrulanırsa, nesnenin XmlSchemaInfo (belirtildiyse) IsDefault, MemberTypeve Validity özellikleri doğrulama sonuçlarıyla ayarlanır.

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ValidateEndElement(XmlSchemaInfo, Object)

Kaynak:
XmlSchemaValidator.cs
Kaynak:
XmlSchemaValidator.cs
Kaynak:
XmlSchemaValidator.cs

Belirtilen öğenin metin içeriğinin veri türüne göre geçerli olup olmadığını doğrular.

C#
public object? ValidateEndElement (System.Xml.Schema.XmlSchemaInfo? schemaInfo, object typedValue);
C#
public object ValidateEndElement (System.Xml.Schema.XmlSchemaInfo schemaInfo, object typedValue);

Parametreler

schemaInfo
XmlSchemaInfo

XmlSchemaInfo Öğesinin metin içeriğinin başarılı bir şekilde doğrulanmasıyla özellikleri ayarlanan bir nesne. Bu parametre olabilir null.

typedValue
Object

öğesinin yazılan metin içeriği.

Döndürülenler

Öğesinin ayrıştırılmış, yazılan basit içeriği.

Özel durumlar

Öğenin metin içeriği geçerli değil.

Yöntem ValidateEndElement doğru sırada çağrılmıyordu (örneğin, yöntemi çağrıldıktan SkipToEndElement(XmlSchemaInfo)sonra çağrılırsaValidateEndElement), yöntemine ValidateText daha önce çağrılar yapılmıştır veya öğe karmaşık içeriğe sahiptir.

Yazılan metin içeriği parametresi olamaz null.

Örnekler

Aşağıdaki örnek, dosyayı şemaya contosoBooks.xml göre contosoBooks.xsd doğrular. Örnek, dosyasını seri durumdan contosoBooks.xml çıkararak düğümlerin değerini sınıfının yöntemlerine geçirmek için sınıfını XmlSchemaValidator kullanırXmlSerializer.

C#
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Collections;

namespace Microsoft.Samples.Xml.Schema
{
    class XmlSchemaValidatorExamples
    {
        static void Main()
        {
            // The XML document to deserialize into the XmlSerializer object.
            XmlReader reader = XmlReader.Create("contosoBooks.xml");

            // The XmlSerializer object.
            XmlSerializer serializer = new XmlSerializer(typeof(ContosoBooks));
            ContosoBooks books = (ContosoBooks)serializer.Deserialize(reader);

            // The XmlSchemaSet object containing the schema used to validate the XML document.
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add("http://www.contoso.com/books", "contosoBooks.xsd");

            // The XmlNamespaceManager object used to handle namespaces.
            XmlNamespaceManager manager = new XmlNamespaceManager(reader.NameTable);

            // Assign a ValidationEventHandler to handle schema validation warnings and errors.
            XmlSchemaValidator validator = new XmlSchemaValidator(reader.NameTable, schemaSet, manager, XmlSchemaValidationFlags.None);
            validator.ValidationEventHandler += new ValidationEventHandler(SchemaValidationEventHandler);

            // Initialize the XmlSchemaValidator object.
            validator.Initialize();

            // Validate the bookstore element, verify that all required attributes are present
            // and prepare to validate child content.
            validator.ValidateElement("bookstore", "http://www.contoso.com/books", null);
            validator.GetUnspecifiedDefaultAttributes(new ArrayList());
            validator.ValidateEndOfAttributes(null);

            // Get the next exptected element in the bookstore context.
            XmlSchemaParticle[] particles = validator.GetExpectedParticles();
            XmlSchemaElement nextElement = particles[0] as XmlSchemaElement;
            Console.WriteLine("Expected Element: '{0}'", nextElement.Name);

            foreach (BookType book in books.Book)
            {
                // Validate the book element.
                validator.ValidateElement("book", "http://www.contoso.com/books", null);

                // Get the exptected attributes for the book element.
                Console.Write("\nExpected attributes: ");
                XmlSchemaAttribute[] attributes = validator.GetExpectedAttributes();
                foreach (XmlSchemaAttribute attribute in attributes)
                {
                    Console.Write("'{0}' ", attribute.Name);
                }
                Console.WriteLine();

                // Validate the genre attribute and display its post schema validation information.
                if (book.Genre != null)
                {
                    validator.ValidateAttribute("genre", "", book.Genre, schemaInfo);
                }
                DisplaySchemaInfo();

                // Validate the publicationdate attribute and display its post schema validation information.
                if (book.PublicationDate != null)
                {
                    validator.ValidateAttribute("publicationdate", "", dateTimeGetter(book.PublicationDate), schemaInfo);
                }
                DisplaySchemaInfo();

                // Validate the ISBN attribute and display its post schema validation information.
                if (book.Isbn != null)
                {
                    validator.ValidateAttribute("ISBN", "", book.Isbn, schemaInfo);
                }
                DisplaySchemaInfo();

                // After validating all the attributes for the current element with ValidateAttribute method,
                // you must call GetUnspecifiedDefaultAttributes to validate the default attributes.
                validator.GetUnspecifiedDefaultAttributes(new ArrayList());

                // Verify that all required attributes of the book element are present
                // and prepare to validate child content.
                validator.ValidateEndOfAttributes(null);

                // Validate the title element and its content.
                validator.ValidateElement("title", "http://www.contoso.com/books", null);
                validator.ValidateEndElement(null, book.Title);

                // Validate the author element, verify that all required attributes are present
                // and prepare to validate child content.
                validator.ValidateElement("author", "http://www.contoso.com/books", null);
                validator.GetUnspecifiedDefaultAttributes(new ArrayList());
                validator.ValidateEndOfAttributes(null);

                if (book.Author.Name != null)
                {
                    // Validate the name element and its content.
                    validator.ValidateElement("name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.Name);
                }

                if (book.Author.FirstName != null)
                {
                    // Validate the first-name element and its content.
                    validator.ValidateElement("first-name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.FirstName);
                }

                if (book.Author.LastName != null)
                {
                    // Validate the last-name element and its content.
                    validator.ValidateElement("last-name", "http://www.contoso.com/books", null);
                    validator.ValidateEndElement(null, book.Author.LastName);
                }

                // Validate the content of the author element.
                validator.ValidateEndElement(null);

                // Validate the price element and its content.
                validator.ValidateElement("price", "http://www.contoso.com/books", null);
                validator.ValidateEndElement(null, book.Price);

                // Validate the content of the book element.
                validator.ValidateEndElement(null);
            }

            // Validate the content of the bookstore element.
            validator.ValidateEndElement(null);

            // Close the XmlReader object.
            reader.Close();
        }

        static XmlSchemaInfo schemaInfo = new XmlSchemaInfo();
        static object dateTimeGetterContent;

        static object dateTimeGetterHandle()
        {
            return dateTimeGetterContent;
        }

        static XmlValueGetter dateTimeGetter(DateTime dateTime)
        {
            dateTimeGetterContent = dateTime;
            return new XmlValueGetter(dateTimeGetterHandle);
        }

        static void DisplaySchemaInfo()
        {
            if (schemaInfo.SchemaElement != null)
            {
                Console.WriteLine("Element '{0}' with type '{1}' is '{2}'",
                    schemaInfo.SchemaElement.Name, schemaInfo.SchemaType, schemaInfo.Validity);
            }
            else if (schemaInfo.SchemaAttribute != null)
            {
                Console.WriteLine("Attribute '{0}' with type '{1}' is '{2}'",
                    schemaInfo.SchemaAttribute.Name, schemaInfo.SchemaType, schemaInfo.Validity);
            }
        }

        static void SchemaValidationEventHandler(object sender, ValidationEventArgs e)
        {
            switch (e.Severity)
            {
                case XmlSeverityType.Error:
                    Console.WriteLine("\nError: {0}", e.Message);
                    break;
                case XmlSeverityType.Warning:
                    Console.WriteLine("\nWarning: {0}", e.Message);
                    break;
            }
        }
    }

    [XmlRootAttribute("bookstore", Namespace = "http://www.contoso.com/books", IsNullable = false)]
    public class ContosoBooks
    {
        [XmlElementAttribute("book")]
        public BookType[] Book;
    }

    public class BookType
    {
        [XmlAttributeAttribute("genre")]
        public string Genre;

        [XmlAttributeAttribute("publicationdate", DataType = "date")]
        public DateTime PublicationDate;

        [XmlAttributeAttribute("ISBN")]
        public string Isbn;

        [XmlElementAttribute("title")]
        public string Title;

        [XmlElementAttribute("author")]
        public BookAuthor Author;

        [XmlElementAttribute("price")]
        public Decimal Price;
    }

    public class BookAuthor
    {
        [XmlElementAttribute("name")]
        public string Name;

        [XmlElementAttribute("first-name")]
        public string FirstName;

        [XmlElementAttribute("last-name")]
        public string LastName;
    }
}

Örnek, dosyayı giriş olarak alır contosoBooks.xml .

XML
<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>

Örnek ayrıca giriş olarak öğesini contosoBooks.xsd alır.

XML
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Açıklamalar

Önemli

, xs:keyrefveya xs:unique kimlik kısıtlamalarına xs:keykarşı doğrulanıyorsanız, yöntem aşırı yüklemesini ValidateEndElement kullanmayın. Bunun yerine yöntemini çağırın ValidateText ve öğesinin değerini geçirin. Ardından, öğesinin metin içeriğinin ValidateEndElement veri türüne göre geçerli olduğunu doğrulamak için bağımsız değişken olarak alan XmlSchemaInfo yöntem aşırı yüklemesini çağırın.

Yöntemi kullanılırken ValidateEndElement dikkate alınması gereken önemli notlar aşağıdadır.

Ayrıca bkz.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1