XmlSchemaException 類別

定義

傳回結構描述例外狀況的詳細資訊。Returns detailed information about the schema exception.

public ref class XmlSchemaException : SystemException
[System.Serializable]
public class XmlSchemaException : SystemException
type XmlSchemaException = class
    inherit SystemException
Public Class XmlSchemaException
Inherits SystemException
繼承
XmlSchemaException
衍生
屬性

範例

下列範例示範如何使用 XmlSchemaException 類別。The following example shows the use of the XmlSchemaException class.

#using <mscorlib.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

class ValidXsd
{
public:

    static void Main()
    {
        FileStream^ fs;
        XmlSchema^ schema;

        try
        {
            fs = gcnew FileStream("example.xsd", FileMode::Open);
            schema = XmlSchema::Read(fs, gcnew ValidationEventHandler(ShowCompileError));

            XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
            schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ShowCompileError);
            schemaSet->Add(schema);
            schemaSet->Compile();

            XmlSchema^ compiledSchema;

            for each (XmlSchema^ schema1 in schemaSet->Schemas())
            {
                compiledSchema = schema1;
            }

            schema = compiledSchema;

            if (schema->IsCompiled)
            {
                // Schema is successfully compiled. 
                // Do something with it here.

            }
        }
        catch (XmlSchemaException^ e)
        {
            Console::WriteLine("LineNumber = {0}", e->LineNumber);
            Console::WriteLine("LinePosition = {0}", e->LinePosition);
            Console::WriteLine("Message = {0}", e->Message);
        }

    }

    static void ShowCompileError(Object^ sender, ValidationEventArgs^ e)
    {
        Console::WriteLine("Validation Error: {0}", e->Message);
    }
};

int main()
{
    ValidXsd::Main();
    Console::ReadLine();
    return 0;
};
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

public class ValidXSD
{
    public static int Main()
    {

        FileStream fs;
        XmlSchema schema;
        try
        {
            fs = new FileStream("example.xsd", FileMode.Open);
            schema = XmlSchema.Read(fs, new ValidationEventHandler(ShowCompileError));

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.ValidationEventHandler += new ValidationEventHandler(ShowCompileError);
            schemaSet.Add(schema);
            schemaSet.Compile();

            XmlSchema compiledSchema = null;

            foreach (XmlSchema schema1 in schemaSet.Schemas())
            {
                compiledSchema = schema1;
            }

            schema = compiledSchema;

            if (schema.IsCompiled)
            {
                // Schema is successfully compiled. 
                // Do something with it here.
            }
            return 0;
        }
        catch (XmlSchemaException e)
        {
            Console.WriteLine("LineNumber = {0}", e.LineNumber);
            Console.WriteLine("LinePosition = {0}", e.LinePosition);
            Console.WriteLine("Message = {0}", e.Message);
            return -1;
        }
    }

    private static void ShowCompileError(object sender, ValidationEventArgs e)
    {
        Console.WriteLine("Validation Error: {0}", e.Message);
    }
}
Option Strict On
Option Explicit On

Imports System.Xml
Imports System.Xml.Schema
Imports System.IO

Public Class ValidXSD
    Public Shared Sub Main()
        Dim fs As FileStream
        Dim schema As XmlSchema
        Try
            fs = New FileStream("example.xsd", FileMode.Open)
            schema = XmlSchema.Read(fs, New ValidationEventHandler(AddressOf ShowCompileError))

            Dim schemaSet As New XmlSchemaSet()
            AddHandler schemaSet.ValidationEventHandler, AddressOf ShowCompileError

            schemaSet.Add(schema)
            schemaSet.Compile()

            Dim compiledSchema As XmlSchema = Nothing

            For Each schema1 As XmlSchema In schemaSet.Schemas()
                compiledSchema = schema1
            Next

            schema = compiledSchema

            If schema.IsCompiled Then
                ' Schema is successfully compiled. 
                ' Do something with it here.
            End If

        Catch e As XmlSchemaException
            Console.WriteLine("LineNumber = {0}", e.LineNumber)
            Console.WriteLine("LinePosition = {0}", e.LinePosition)
            Console.WriteLine("Message = {0}", e.Message)
            Console.WriteLine("Source = {0}", e.Source)

        End Try
    End Sub


    Private Shared Sub ShowCompileError(ByVal sender As Object, ByVal e As ValidationEventArgs)
        Console.WriteLine("Validation Error: {0}", e.Message)
    End Sub
End Class

備註

重要

XmlSchemaException 類別可能包含不應該在未受信任的案例中公開的機密資訊。The XmlSchemaException class may contain sensitive information that should not be exposed in untrusted scenarios. 例如,SourceUri 屬性會傳回造成例外狀況之架構檔案的 URI 路徑。For example, the SourceUri property returns the URI path to the schema file that caused the exception. 不應在未受信任的案例中公開 SourceUri 屬性。The SourceUri property should not be exposed in untrusted scenarios. 應該適當地處理例外狀況,如此一來,就不會在未受信任的案例中公開此機密資訊。Exceptions should be properly handled so that this sensitive information is not exposed in untrusted scenarios.

建構函式

XmlSchemaException()

初始化 XmlSchemaException 類別的新執行個體。Initializes a new instance of the XmlSchemaException class.

XmlSchemaException(SerializationInfo, StreamingContext)

以指定的 XmlSchemaExceptionSerializationInfo 資訊建構包含 StreamingContext 所有屬性的新 XmlSchemaException 物件。Constructs a new XmlSchemaException object with the given SerializationInfo and StreamingContext information that contains all the properties of the XmlSchemaException.

XmlSchemaException(String)

使用指定的例外狀況訊息,初始化 XmlSchemaException 類別的新執行個體。Initializes a new instance of the XmlSchemaException class with the exception message specified.

XmlSchemaException(String, Exception)

使用例外狀況訊息和導致這個指定之例外狀況的原始 XmlSchemaException 物件,初始化 Exception 類別的新執行個體。Initializes a new instance of the XmlSchemaException class with the exception message and original Exception object that caused this exception specified.

XmlSchemaException(String, Exception, Int32, Int32)

使用指定的例外狀況訊息,以及導致這個指定之例外狀況的原始 XmlSchemaException 物件、XML 中的行號和行位置,初始化 Exception 類別的新執行個體。Initializes a new instance of the XmlSchemaException class with the exception message specified, and the original Exception object, line number, and line position of the XML that cause this exception specified.

屬性

Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
LineNumber

取得行號,指出發生錯誤的位置。Gets the line number indicating where the error occurred.

LinePosition

取得行位置,指出發生錯誤的位置。Gets the line position indicating where the error occurred.

Message

取得這個例外狀況之錯誤條件的描述。Gets the description of the error condition of this exception.

Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)
SourceSchemaObject

產生 XmlSchemaObjectXmlSchemaExceptionThe XmlSchemaObject that produced the XmlSchemaException.

SourceUri

取得導致該例外狀況之結構描述的統一資源識別元 (URI) 位置。Gets the Uniform Resource Identifier (URI) location of the schema that caused the exception.

StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

將所有 XmlSchemaException 屬性資料流至指定之 SerializationInfoStreamingContext 類別。Streams all the XmlSchemaException properties into the SerializationInfo class for the given StreamingContext.

GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(繼承來源 Exception)

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於