搭配使用內嵌 XML 結構描述與 XmlReader 進行驗證
更新: November 2007
您可以根據內嵌 XML 結構描述定義語言 (XSD) 結構描述驗證 XML 檔案。建立 XmlReader 之前,請變更 XmlReaderSettings.ValidationFlags 屬性以啟用 ProcessInlineSchema 選項。
注意事項: |
---|
由於內嵌結構描述是以根項目的項目子系出現,因此當執行內嵌結構描述驗證時,無法驗證根項目。並且會針對根項目產生驗證警告。 |
範例
下列範例會使用內嵌 XML 結構描述驗證 XML 檔案。因為讀取器已設定為必須顯示驗證警告,所以您還會在根項目上看到預期的警告。
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
public class ValidXSD
public shared sub Main()
' Set the validation settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ProcessInlineSchema
settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("inlineSchema.xml", settings)
' Parse the file.
while (reader.Read())
end while
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
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class ValidXSD {
public static void Main() {
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
// Parse the file.
while (reader.Read());
}
// 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);
}
}
輸入
該範例採用 inlineSchema.xml 檔案做為輸入。
<root>
<!--Start of schema-->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
xmlns='xsdHeadCount'
targetNamespace='xsdHeadCount'>
<xs:element name='HeadCount'>
<xs:complexType>
<xs:sequence>
<xs:element name='ID' type='xs:unsignedShort' maxOccurs='unbounded' />
</xs:sequence>
<xs:attribute name='division' type='xs:string' use='optional' default='QA'/>
</xs:complexType>
</xs:element>
</xs:schema>
<!--End of schema-->
<hc:HeadCount xmlns:hc='xsdHeadCount'>
<ID>12365</ID>
<ID>43295</ID>
<division>Accounting</division>
</hc:HeadCount>
</root>
輸出
警告: 找不到相符的結構描述。未發生驗證。找不到項目 root 的結構描述資訊。
驗證錯誤: 項目 xsdHeadCount:HeadCount 含有無效的項目子系 division。預期的是 'ID'。