XMLがあります:
<?xml version="1.0" encoding="utf-8" ?>
<FirstSection FirstSectionAttr="5" >
<SecondSection Value="0x15"/>
<SecondSection Value="10"/>
</FirstSection>
XSDがあります(VSによって作成されました):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="FirstSection">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="SecondSection">
<xs:complexType>
<xs:attribute name="Value" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="FirstSectionAttr" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
検証するコードがあります:
static void Validate(string xsdPath, string fullFileName)
{
try
{
var settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", xsdPath);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += OnXmlValidationEventError;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
using (var reader = XmlReader.Create(fullFileName, settings))
{
while (reader.Read())
{
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private static void OnXmlValidationEventError(object sender, ValidationEventArgs e)
{
try
{
Console.WriteLine("Problem: " + e.Message);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
コードは次を返します。
問題: 要素「FirstSection」のスキーマ情報が見つかりませんでした。
問題: 属性「FirstSectionAttr」のスキーマ情報が見つかりませんでした。
問題: 要素 'SecondSection' のスキーマ情報が見つかりませんでした。
問題: 属性「値」のスキーマ情報が見つかりませんでした。
問題: 要素 'SecondSection' のスキーマ情報が見つかりませんでした。
問題: 属性「値」のスキーマ情報が見つかりませんでした。
それを正しく検証する方法は?