ValidationEventArgs.Severity プロパティ
検証イベントの重大度レベルを取得します。
Public ReadOnly Property Severity As XmlSeverityType
[C#]
public XmlSeverityType Severity {get;}
[C++]
public: __property XmlSeverityType get_Severity();
[JScript]
public function get Severity() : XmlSeverityType;
プロパティ値
検証イベントの重大度レベルを表す XmlSeverityType 値。
使用例
XML ファイルを検証して、検出されたエラーまたは警告を生成する例を次に示します。
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
public class Sample
public shared sub Main ()
'Load the schema collection.
Dim xsc as XmlSchemaCollection = new XmlSchemaCollection()
xsc.Add("urn:bookstore-schema", "books.xsd")
'Validate the file using the schema stored in the collection.
'Any elements belonging to the namespace "urn:cd-schema" generate
'a warning because there is no schema matching that namespace.
Validate("store.xml", xsc)
end sub
private shared sub Validate(filename as String, xsc as XmlSchemaCollection)
Console.WriteLine()
Console.WriteLine("Validating XML file {0}...", filename.ToString())
Dim reader as XmlTextReader = new XmlTextReader (filename)
Dim vreader as XmlValidatingReader =new XmlValidatingReader (reader)
vreader.ValidationType = ValidationType.Schema
vreader.Schemas.Add(xsc)
'Set the validation event handler.
AddHandler vreader.ValidationEventHandler, AddressOf ValidationCallBack
'Read the XML data.
while (vreader.Read())
end while
'Close the reader.
vreader.Close()
end sub
'Display any warnings or errors.
Private shared sub ValidationCallBack (sender as object, args as ValidationEventArgs)
if (args.Severity=XmlSeverityType.Warning)
Console.WriteLine(" Warning: Matching schema not found. No validation occurred." + args.Message)
else
Console.WriteLine(" Validation error: " + args.Message)
end if
end sub
end class
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class Sample
{
public static void Main ()
{
//Load the schema collection.
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add("urn:bookstore-schema", "books.xsd");
//Validate the file using the schema stored in the collection.
//Any elements belonging to the namespace "urn:cd-schema" generate
//a warning because there is no schema matching that namespace.
Validate("store.xml", xsc);
}
private static void Validate(String filename, XmlSchemaCollection xsc)
{
Console.WriteLine();
Console.WriteLine("\r\nValidating XML file {0}...", filename.ToString());
XmlTextReader reader = new XmlTextReader (filename);
XmlValidatingReader vreader=new XmlValidatingReader (reader);
vreader.ValidationType = ValidationType.Schema;
vreader.Schemas.Add(xsc);
//Set the validation event handler.
vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
//Read the XML data.
while (vreader.Read()){}
//Close the reader.
vreader.Close();
}
//Display any warnings or errors.
private static void ValidationCallBack (object sender, ValidationEventArgs args)
{
if (args.Severity==XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
__gc public class Sample
{
public:
static void Validate(String * filename, XmlSchemaCollection * xsc)
{
Console::WriteLine();
Console::WriteLine(S"\r\nValidating XML file {0}...", filename);
XmlTextReader* reader = new XmlTextReader (filename);
XmlValidatingReader* vreader = new XmlValidatingReader (reader);
vreader -> ValidationType = ValidationType::Schema;
vreader -> Schemas->Add(xsc);
//Set the validation event handler.
vreader -> ValidationEventHandler += new ValidationEventHandler (vreader, ValidationCallBack);
//Read the XML data.
while (vreader -> Read()) {}
// Close the reader.
vreader -> Close();
}
// Display any warnings or errors.
static void ValidationCallBack (Object* sender, ValidationEventArgs * args)
{
if (args -> Severity==XmlSeverityType::Warning)
Console::WriteLine(S"\tWarning: Matching schema not found. No validation occurred. {0}", args -> Message);
else
Console::WriteLine(S"\tValidation error: {0}", args -> Message);
}
};
int main ()
{
//Load the schema collection.
XmlSchemaCollection* xsc = new XmlSchemaCollection();
xsc -> Add(S"urn:bookstore-schema", S"books.xsd");
//Validate the file using the schema stored in the collection.
//Any elements belonging to the namespace S"urn:cd-schema" generate
//a warning because there is no schema matching that namespace.
Sample * MySample = new Sample();
MySample -> Validate(S"store.xml", xsc);
}
[JScript]
import System
import System.IO
import System.Xml
import System.Xml.Schema
public class Sample
{
public static function Main ()
{
//Load the schema collection.
var xsc : XmlSchemaCollection = new XmlSchemaCollection();
xsc.Add("urn:bookstore-schema", "books.xsd");
//Validate the file using the schema stored in the collection.
//Any elements belonging to the namespace "urn:cd-schema" generate
//a warning because there is no schema matching that namespace.
Validate("store.xml", xsc);
}
private static function Validate(filename : String, xsc : XmlSchemaCollection)
{
Console.WriteLine();
Console.WriteLine("\r\nValidating XML file {0}...", filename.ToString());
var reader : XmlTextReader = new XmlTextReader (filename);
var vreader : XmlValidatingReader = new XmlValidatingReader (reader);
vreader.ValidationType = ValidationType.Schema;
vreader.Schemas.Add(xsc);
//Set the validation event handler.
vreader.add_ValidationEventHandler(ValidationCallBack);
//Read the XML data.
while (vreader.Read()){}
//Close the reader.
vreader.Close();
}
//Display any warnings or errors.
private static function ValidationCallBack (sender, args : ValidationEventArgs)
{
if (args.Severity==XmlSeverityType.Warning)
Console.WriteLine("\tWarning: Matching schema not found. No validation occurred." + args.Message);
else
Console.WriteLine("\tValidation error: " + args.Message);
}
}
前述の例では、次の入力ファイルを使用しています。
store.xml
<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema" xmlns:cd="urn:cd-schema">
<book genre="novel">
<title>The Confidence Man</title>
<price>11.99</price>
</book>
<cd:cd>
<title>Americana</title>
<cd:artist>Offspring</cd:artist>
<price>16.95</price>
</cd:cd>
</bookstore>
books.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="urn:bookstore-schema"
elementFormDefault="qualified"
targetNamespace="urn:bookstore-schema">
<xsd:element name="bookstore" type="bookstoreType"/>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="authorName"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string"/>
<xsd:element name="last-name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
ValidationEventArgs クラス | ValidationEventArgs メンバ | System.Xml.Schema 名前空間