Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XmlReaderSettings の ValidationEventHandler で詳細な内容が取りたい

12 views
Skip to first unread message

Tak

unread,
Jul 6, 2007, 6:32:02 AM7/6/07
to
http://support.microsoft.com/kb/307379/ja
を元に Xmlの Validation をチェックしようとしていますが、
XmlReaderSettings の ValidationEventHandler がおきた時に
詳しいエラー内容を把握するにはどうしたら良いのでしょうか?

プロジェクトに xmlと xsd を追加して xmlを編集しようとすると
かなり詳細に警告が出るのですが、それに相当することをコードで書きたいのですが。

using System;
using System.Xml;
using System.Xml.Schema;

class XmlValidation
{
private static bool isValid = true;

static void Main(string[] args)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags =
XmlSchemaValidationFlags.ProcessSchemaLocation
|
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler);

XmlReader reader = XmlReader.Create("Product.xml", settings);
while (reader.Read()) { }
reader.Close();

Console.WriteLine("Xml Validation result : " + isValid);
}

public static void MyValidationEventHandler(object sender
, ValidationEventArgs args)
{
isValid = false;
XmlReader reader = (XmlReader)sender;
//Console.WriteLine(args.Message); // 既定のメッセージではなく
Console.WriteLine("{0} が期待する (?{1}?) に反して {2} である[{3}]"
, reader.Name
, "???" //reader.NodeType.GetType().Name
, reader.ValueType.Name
, reader.Value); // もっと詳細を知りたい
}
}

0 new messages