Extensions.Validate 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
验证 XDocument、XElement 或 XAttribute 是否与 XmlSchemaSet 中的 XSD 相符。
重载
Validate(XDocument, XmlSchemaSet, ValidationEventHandler) |
此方法验证 XDocument 是否与 XmlSchemaSet 中的 XSD 相符。 |
Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler) |
此方法验证 XAttribute 是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符。 |
Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean) |
验证 XDocument 是否与 XmlSchemaSet 中的某个 XSD 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。 |
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler) |
此方法验证 XElement 子树是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符。 |
Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean) |
验证 XAttribute 是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。 |
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean) |
验证 XElement 子树是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。 |
注解
这些方法使用基础 XmlReader 对 XSD 验证 XML 树。
使用 ValidationEventHandler 委托处理验证错误和警告消息。 如果未向这些方法提供事件处理程序,验证错误将作为一个 XmlSchemaValidationException公开。 验证警告不会引发 XmlSchemaValidationException 。
其中一些扩展方法(可选)允许填充后架构验证信息集 (PSVI) 。
Validate(XDocument, XmlSchemaSet, ValidationEventHandler)
此方法验证 XDocument 是否与 XmlSchemaSet 中的 XSD 相符。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XDocument ^ source, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler);
public static void Validate (this System.Xml.Linq.XDocument source, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler);
public static void Validate (this System.Xml.Linq.XDocument source, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler);
static member Validate : System.Xml.Linq.XDocument * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler -> unit
<Extension()>
Public Sub Validate (source As XDocument, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler)
参数
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
下面的示例创建一个 XmlSchemaSet,然后针对架构集验证两个 XDocument 对象。 其中一个文档为有效文档,而另一个则不是。
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XElement("Child1", "content1"),
new XElement("Child2", "content1")
)
);
XDocument doc2 = new XDocument(
new XElement("Root",
new XElement("Child1", "content1"),
new XElement("Child3", "content1")
)
);
Console.WriteLine("Validating doc1");
bool errors = false;
doc1.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
});
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Validating doc2");
errors = false;
doc2.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
});
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated");
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child2>content2</Child2>
</Root>
Dim doc2 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>
Console.WriteLine("Validating doc1")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating doc2")
errors = False
doc2.Validate(schemas, AddressOf XSDErrors)
Console.WriteLine("doc2 {0}", IIf(errors, "did not validate", "validated"))
End Sub
该示例产生下面的输出:
Validating doc1
doc1 validated
Validating doc2
The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.
doc2 did not validate
注解
此扩展方法验证是否XDocument符合架构内容模型。XmlSchemaSet
适用于
Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)
此方法验证 XAttribute 是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XAttribute ^ source, System::Xml::Schema::XmlSchemaObject ^ partialValidationType, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler);
public static void Validate (this System.Xml.Linq.XAttribute source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler);
public static void Validate (this System.Xml.Linq.XAttribute source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler);
static member Validate : System.Xml.Linq.XAttribute * System.Xml.Schema.XmlSchemaObject * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler -> unit
<Extension()>
Public Sub Validate (source As XAttribute, partialValidationType As XmlSchemaObject, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler)
参数
- source
- XAttribute
要验证的 XAttribute。
- partialValidationType
- XmlSchemaObject
一个 XmlSchemaObject,指定要验证的子树。
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Lang' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='C#'/>
<xsd:enumeration value='VB'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XAttribute("Lang", "C#")
)
);
Console.WriteLine("Validating doc1 ...");
bool errors = false;
doc1.Validate(schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Validating Lang attribute ...");
XAttribute lang = doc1.Root.Attribute("Lang");
errors = false;
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
});
Console.WriteLine("lang {0}", errors ? "did not validate" : "validated");
// the following makes the Lang attribute invalid according to the schema
lang.Value = "VC";
Console.WriteLine();
Console.WriteLine("Validating Lang attribute ...");
errors = false;
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
});
Console.WriteLine("lang {0}", errors ? "did not validate" : "validated");
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Lang' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='C#'/>
<xsd:enumeration value='VB'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = <?xml version='1.0'?>
<Root Lang='C#'/>
Console.WriteLine("Validating doc1 ...")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating Lang attribute ...")
Dim lang As XAttribute = doc1.Root.Attribute("Lang")
errors = False
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors)
Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
' the following makes the Lang attribute invalid according to the schema
lang.Value = "VC"
Console.WriteLine()
Console.WriteLine("Validating Lang attribute ...")
errors = False
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors)
Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
End Sub
该示例产生下面的输出:
Validating doc1 ...
doc1 validated
Validating Lang attribute ...
lang validated
Validating Lang attribute ...
The 'Lang' attribute is invalid - The value 'VC' is invalid according to its datatype 'Token' - The Enumeration constraint failed.
lang did not validate
注解
可以使用此方法验证是否符合 XAttribute 架构。 修改属性后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只需要更少的处理时间来验证该属性。
如果传递 null
, validationEventHandler
此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证属性,请使用 . 的 XmlSchemaObject实例。 可以通过各种方式获取此实例。 一种简单方法如下所示:
验证文档是否符合架构。
通过调用 Validate 扩展方法 (PSVI) 添加架构验证后信息集。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的值XmlSchemaObjectXElement,则类型将为 XmlSchemaElement。
如果获取某个类型的值XmlSchemaObjectXAttribute,则类型将为 XmlSchemaAttribute。
拥有实例 XmlSchemaObject后,可以使用此方法来验证属性。
适用于
Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)
验证 XDocument 是否与 XmlSchemaSet 中的某个 XSD 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XDocument ^ source, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XDocument source, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XDocument source, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler, bool addSchemaInfo);
static member Validate : System.Xml.Linq.XDocument * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler * bool -> unit
<Extension()>
Public Sub Validate (source As XDocument, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler, addSchemaInfo As Boolean)
参数
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
以下示例包含一个 XSD,该 Child2
XSD 定义具有 Att1
默认值的属性的元素。 成功验证文档后,将具有默认值的属性添加到 XML 树中。 请注意,不会向 doc2
默认属性添加,该属性不会针对架构进行验证。
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Att1' default='Att1 Default Value'/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XElement("Child1", "c1"),
new XElement("Child2", "c2")
)
);
XDocument doc2 = new XDocument(
new XElement("Root",
new XElement("Child1", "content1"),
new XElement("Child3", "content1")
)
);
Console.WriteLine("Validating doc1");
bool errors = false;
doc1.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Validating doc2");
errors = false;
doc2.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
}, true);
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Contents of doc1:");
Console.WriteLine(doc1);
Console.WriteLine();
Console.WriteLine("Contents of doc2:");
Console.WriteLine(doc2);
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Att1' default='Att1 Default Value'/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = <?xml version='1.0'?>
<Root>
<Child1>c1</Child1>
<Child2>c2</Child2>
</Root>
Dim doc2 As XDocument = <?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>
Console.WriteLine("Validating doc1")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating doc2")
errors = False
doc2.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc2 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Contents of doc1:")
Console.WriteLine(doc1)
Console.WriteLine()
Console.WriteLine("Contents of doc2:")
Console.WriteLine(doc2)
End Sub
该示例产生下面的输出:
Validating doc1
doc1 validated
Validating doc2
The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.
doc2 did not validate
Contents of doc1:
<Root>
<Child1>c1</Child1>
<Child2 Att1="Att1 Default Value">c2</Child2>
</Root>
Contents of doc2:
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>
以下示例使用 PSVI 填充树。 验证后,它会打印树中的所有元素和属性,这些元素和属性根据 PSVI 无效。
static void DumpInvalidNodes(XElement el)
{
if (el.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Element {0}",
el.AncestorsAndSelf()
.InDocumentOrder()
.Aggregate("", (s, i) => s + "/" + i.Name.ToString()));
foreach (XAttribute att in el.Attributes())
if (att.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Attribute {0}",
att
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Aggregate("",
(s, i) => s + "/" + i.Name.ToString()) + "/@" + att.Name.ToString()
);
foreach (XElement child in el.Elements())
DumpInvalidNodes(child);
}
static void Main(string[] args)
{
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:simpleType name='GCType'>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='AAA'/>
<xsd:enumeration value='BBB'/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='GrandChild1' type='GCType'/>
<xsd:element name='GrandChild2' type='GCType'/>
<xsd:element name='GrandChild3' type='GCType'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XElement("Child1",
new XElement("GrandChild1", "AAA"),
new XElement("GrandChild2", "ZZZ"),
new XElement("GrandChild3", "ZZZ")
)
)
);
Console.WriteLine("Validating doc1 ...");
bool errors = false;
doc1.Validate(schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
DumpInvalidNodes(doc1.Root);
}
Private Sub DumpInvalidNodes(ByVal el As XElement)
If el.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
Console.WriteLine("Invalid Element {0}", _
el _
.AncestorsAndSelf _
.InDocumentOrder() _
.Aggregate("", _
Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()))
End If
For Each att As XAttribute In el.Attributes()
If att.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
Console.WriteLine("Invalid Attribute {0}", _
att _
.Parent _
.AncestorsAndSelf() _
.InDocumentOrder() _
.Aggregate("", _
Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()) + _
"/@" + att.Name.ToString())
End If
Next
For Each child As XElement In el.Elements()
DumpInvalidNodes(child)
Next
End Sub
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:simpleType name='GCType'>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='AAA'/>
<xsd:enumeration value='BBB'/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='GrandChild1' type='GCType'/>
<xsd:element name='GrandChild2' type='GCType'/>
<xsd:element name='GrandChild3' type='GCType'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>
<GrandChild1>AAA</GrandChild1>
<GrandChild2>ZZZ</GrandChild2>
<GrandChild3>ZZZ</GrandChild3>
</Child1>
</Root>
Console.WriteLine("Validating doc1 ...")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
DumpInvalidNodes(doc1.Root)
End Sub
该示例产生下面的输出:
Validating doc1 ...
The 'GrandChild2' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed.
The 'GrandChild3' element is invalid - The value 'ZZZ' is invalid according to its datatype 'GCType' - The Enumeration constraint failed.
doc1 did not validate
Invalid Element /Root
Invalid Element /Root/Child1
Invalid Element /Root/Child1/GrandChild2
Invalid Element /Root/Child1/GrandChild3
注解
此扩展方法验证是否XDocument符合架构内容模型。XmlSchemaSet
true
如果是addSchemaInfo
,此方法使用架构验证后信息集填充 XML 树, (PSVI) 。
使用 PSVI 填充 XML 树有两个步骤。
首先,将注释添加到树中的所有节点,以便调用或Extensions.GetSchemaInfo调用Extensions.GetSchemaInfo树中的元素或属性。
其次,XSD 中定义的默认元素和属性将添加到 XML 树中。 通过调用其中 GetSchemaInfo 一种方法,可以确定是否从 XSD 添加特定元素或属性作为默认元素或属性。
适用于
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)
此方法验证 XElement 子树是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XElement ^ source, System::Xml::Schema::XmlSchemaObject ^ partialValidationType, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler);
public static void Validate (this System.Xml.Linq.XElement source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler);
public static void Validate (this System.Xml.Linq.XElement source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler);
static member Validate : System.Xml.Linq.XElement * System.Xml.Schema.XmlSchemaObject * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler -> unit
<Extension()>
Public Sub Validate (source As XElement, partialValidationType As XmlSchemaObject, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler)
参数
- partialValidationType
- XmlSchemaObject
一个 XmlSchemaObject,指定要验证的子树。
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='GrandChild1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='GrandChild2' minOccurs='1' maxOccurs='2'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XElement("Child1",
new XElement("GrandChild1", "gc"),
new XElement("GrandChild2", "gc")
)
)
);
Console.WriteLine("Validating doc1 ...");
bool errors = false;
doc1.Validate(schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Validating Child1 after first edit ...");
XElement child1 = doc1.Element("Root").Element("Child1");
child1.Add(new XElement("GrandChild2", "gc"));
errors = false;
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
});
Console.WriteLine("child1 {0}", errors ? "did not validate" : "validated");
// the following makes the Child1 element invalid according to the schema
child1.Add(new XElement("GrandChild3", "gc"));
Console.WriteLine();
Console.WriteLine("Validating Child1 after second edit ...");
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
});
Console.WriteLine("child1 {0}", errors ? "did not validate" : "validated");
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='GrandChild1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='GrandChild2' minOccurs='1' maxOccurs='2'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>
<GrandChild1>gc</GrandChild1>
<GrandChild2>gc</GrandChild2>
</Child1>
</Root>
Console.WriteLine("Validating doc1 ...")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating Child1 after first edit ...")
Dim child1 As XElement = doc1.Element("Root").Element("Child1")
child1.Add(<GrandChild2>gc</GrandChild2>)
errors = False
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, AddressOf XSDErrors)
Console.WriteLine("child1 {0}", IIf(errors, "did not validate", "validated"))
' the following makes the Child1 element invalid according to the schema
child1.Add(<GrandChild3>gc</GrandChild3>)
Console.WriteLine()
Console.WriteLine("Validating Child1 after second edit ...")
child1.Validate(child1.GetSchemaInfo().SchemaElement, schemas, AddressOf XSDErrors)
Console.WriteLine("child1 {0}", IIf(errors, "did not validate", "validated"))
End Sub
该示例产生下面的输出:
Validating doc1 ...
doc1 validated
Validating Child1 after first edit ...
child1 validated
Validating Child1 after second edit ...
The element 'Child1' has invalid child element 'GrandChild3'.
child1 did not validate
注解
可以使用此方法验证子树 (及其 XElement 根) 是否符合架构。 修改了子树后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只需对子树进行验证需要更少的处理时间。
如果传递 null
, validationEventHandler
此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证子树,请使用一 XmlSchemaObject个实例。 可以通过多种方式获取此实例。 一种简单方法如下:
验证文档是否符合架构。
通过调用 Validate 扩展方法 (PSVI) 添加架构验证后信息集。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的值XmlSchemaObjectXElement,则类型将为 XmlSchemaElement。
如果获取某个类型的值XmlSchemaObjectXAttribute,则类型将为 XmlSchemaAttribute。
拥有子 XmlSchemaObject树的实例后,可以使用此方法验证子树。
适用于
Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)
验证 XAttribute 是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XAttribute ^ source, System::Xml::Schema::XmlSchemaObject ^ partialValidationType, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XAttribute source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XAttribute source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler, bool addSchemaInfo);
static member Validate : System.Xml.Linq.XAttribute * System.Xml.Schema.XmlSchemaObject * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler * bool -> unit
<Extension()>
Public Sub Validate (source As XAttribute, partialValidationType As XmlSchemaObject, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler, addSchemaInfo As Boolean)
参数
- source
- XAttribute
要验证的 XAttribute。
- partialValidationType
- XmlSchemaObject
一个 XmlSchemaObject,指定要验证的子树。
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
static void DumpInvalidNodes(XElement el)
{
if (el.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Element {0}",
el.AncestorsAndSelf()
.InDocumentOrder()
.Aggregate("", (s, i) => s + "/" + i.Name.ToString()));
foreach (XAttribute att in el.Attributes())
if (att.GetSchemaInfo().Validity != XmlSchemaValidity.Valid)
Console.WriteLine("Invalid Attribute {0}",
att
.Parent
.AncestorsAndSelf()
.InDocumentOrder()
.Aggregate("",
(s, i) => s + "/" + i.Name.ToString()) + "/@" + att.Name.ToString()
);
foreach (XElement child in el.Elements())
DumpInvalidNodes(child);
}
static void Main(string[] args)
{
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Lang' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='C#'/>
<xsd:enumeration value='VB'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XAttribute("Lang", "C#")
)
);
Console.WriteLine("Validating doc1 ...");
bool errors = false;
doc1.Validate(schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
DumpInvalidNodes(doc1.Root);
Console.WriteLine();
Console.WriteLine("Validating Lang attribute ...");
XAttribute lang = doc1.Element("Root").Attribute("Lang");
errors = false;
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("lang {0}", errors ? "did not validate" : "validated");
DumpInvalidNodes(doc1.Root);
// the following makes the Lang attribute invalid according to the schema
lang.Value = "VC";
Console.WriteLine();
Console.WriteLine("Validating Lang attribute ...");
errors = false;
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, (sender, e) =>
{
Console.WriteLine(e.Message);
errors = true;
}, true);
Console.WriteLine("lang {0}", errors ? "did not validate" : "validated");
DumpInvalidNodes(doc1.Root);
}
Private Sub DumpInvalidNodes(ByVal el As XElement)
If el.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
Console.WriteLine("Invalid Element {0}", _
el _
.AncestorsAndSelf _
.InDocumentOrder() _
.Aggregate("", _
Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()))
End If
For Each att As XAttribute In el.Attributes()
If att.GetSchemaInfo.Validity <> XmlSchemaValidity.Valid Then
Console.WriteLine("Invalid Attribute {0}", _
att _
.Parent _
.AncestorsAndSelf() _
.InDocumentOrder() _
.Aggregate("", _
Function(ByVal s, ByVal i) s + "/" + i.Name.ToString()) + _
"/@" + att.Name.ToString())
End If
Next
For Each child As XElement In el.Elements()
DumpInvalidNodes(child)
Next
End Sub
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Lang' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:token'>
<xsd:enumeration value='C#'/>
<xsd:enumeration value='VB'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = <?xml version='1.0'?>
<Root Lang='C#'/>
Console.WriteLine("Validating doc1 ...")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
DumpInvalidNodes(doc1.Root)
Console.WriteLine()
Console.WriteLine("Validating Lang attribute ...")
Dim lang As XAttribute = doc1.Element("Root").Attribute("Lang")
errors = False
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors, True)
Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
DumpInvalidNodes(doc1.Root)
' the following makes the Lang attribute invalid according to the schema
lang.Value = "VC"
Console.WriteLine()
Console.WriteLine("Validating Lang attribute ...")
errors = False
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors, True)
Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
DumpInvalidNodes(doc1.Root)
End Sub
该示例产生下面的输出:
Validating doc1 ...
doc1 validated
Validating Lang attribute ...
lang validated
Validating Lang attribute ...
The 'Lang' attribute is invalid - The value 'VC' is invalid according to its datatype 'Token' - The Enumeration constraint failed.
lang did not validate
Invalid Attribute /Root/@Lang
注解
可以使用此方法验证 XAttribute 是否符合架构。 修改属性后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只验证属性需要更少的处理时间。
true
如果是addSchemaInfo
,此方法使用后架构验证信息集填充属性, (PSVI) 。 使用 PSVI 填充 XML 树后,可以调用 Extensions.GetSchemaInfo 已验证的属性。 如果编写的代码依赖于返回 GetSchemaInfo的数据,则这非常有用。
如果传递null``validationEventHandler
,则此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证属性,请使用 . 的 XmlSchemaObject实例。 可以通过多种方式获取此实例。 一种简单方法如下:
验证文档是否符合架构。
通过调用 Validate 扩展方法 (PSVI) 添加架构验证后信息集。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的值XmlSchemaObjectXElement,则类型将为 XmlSchemaElement。
如果获取某个类型的值XmlSchemaObjectXAttribute,则类型将为 XmlSchemaAttribute。
拥有实例 XmlSchemaObject后,可以使用此方法验证属性。
适用于
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)
验证 XElement 子树是否与指定的 XmlSchemaObject 和 XmlSchemaSet 相符,并且可以选择使用架构验证后信息集 (PSVI) 填充 XML 树。
public:
[System::Runtime::CompilerServices::Extension]
static void Validate(System::Xml::Linq::XElement ^ source, System::Xml::Schema::XmlSchemaObject ^ partialValidationType, System::Xml::Schema::XmlSchemaSet ^ schemas, System::Xml::Schema::ValidationEventHandler ^ validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XElement source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler? validationEventHandler, bool addSchemaInfo);
public static void Validate (this System.Xml.Linq.XElement source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler, bool addSchemaInfo);
static member Validate : System.Xml.Linq.XElement * System.Xml.Schema.XmlSchemaObject * System.Xml.Schema.XmlSchemaSet * System.Xml.Schema.ValidationEventHandler * bool -> unit
<Extension()>
Public Sub Validate (source As XElement, partialValidationType As XmlSchemaObject, schemas As XmlSchemaSet, validationEventHandler As ValidationEventHandler, addSchemaInfo As Boolean)
参数
- partialValidationType
- XmlSchemaObject
一个 XmlSchemaObject,指定要验证的子树。
- schemas
- XmlSchemaSet
一个作为验证依据的 XmlSchemaSet。
- validationEventHandler
- ValidationEventHandler
一个 ValidationEventHandler,可用于处理当读取器遇到验证错误时所发生的事件。 如果为 null
,将在验证错误时引发异常。
例外
出现 XML 架构定义语言 (XSD) 验证错误时引发。
示例
string xsdMarkup =
@"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Att1' default='Att1 Default Value'/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsdMarkup)));
XDocument doc1 = new XDocument(
new XElement("Root",
new XElement("Child1", "c1"),
new XElement("Child2", "c2")
)
);
XDocument doc2 = new XDocument(
new XElement("Root",
new XElement("Child1", "content1"),
new XElement("Child3", "content1")
)
);
Console.WriteLine("Validating doc1");
bool errors = false;
doc1.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
}, true);
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Validating doc2");
errors = false;
doc2.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
}, true);
Console.WriteLine("doc2 {0}", errors ? "did not validate" : "validated");
Console.WriteLine();
Console.WriteLine("Contents of doc1:");
Console.WriteLine(doc1);
Console.WriteLine();
Console.WriteLine("Contents of doc2:");
Console.WriteLine(doc2);
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Sub Main()
Dim xsdMarkup As XDocument = _
<?xml version='1.0'?>
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base='xsd:string'>
<xsd:attribute name='Att1' default='Att1 Default Value'/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>c1</Child1>
<Child2>c2</Child2>
</Root>
Dim doc2 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>
Console.WriteLine("Validating doc1")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating doc2")
errors = False
doc2.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc2 {0}", IIf(errors, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Contents of doc1:")
Console.WriteLine(doc1)
Console.WriteLine()
Console.WriteLine("Contents of doc2:")
Console.WriteLine(doc2)
End Sub
该示例产生下面的输出:
Validating doc1
doc1 validated
Validating doc2
The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.
doc2 did not validate
Contents of doc1:
<Root>
<Child1>c1</Child1>
<Child2 Att1="Att1 Default Value">c2</Child2>
</Root>
Contents of doc2:
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>
注解
可以使用此方法验证子树 (与 XElement 子树根目录) 是否符合架构。 修改子树后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只需更少的处理时间来验证子树。
true
如果是addSchemaInfo
,则此方法使用后架构验证信息集填充 XML 树, (PSVI) 。
使用 PSVI 填充 XML 树有两个方面。
首先,将注释添加到树中的所有节点,以便现在可以对树中的元素或属性进行调用 GetSchemaInfo 。
其次,XSD 中定义的默认元素和属性将添加到 XML 树中。 通过调用其中 GetSchemaInfo 一种方法,可以确定是否从 XSD 添加特定元素或属性作为默认元素或属性。
如果传递null``validationEventHandler
,则此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证子树,请使用一 XmlSchemaObject个实例。 可以通过多种方式获取此实例。 一种简单方法如下:
验证文档是否符合架构。
通过调用 Validate 扩展方法 (PSVI) 添加架构验证后信息集。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的值XmlSchemaObjectXElement,则类型将为 XmlSchemaElement。
如果获取某个类型的值XmlSchemaObjectXAttribute,则类型将为 XmlSchemaAttribute。
拥有子 XmlSchemaObject树的实例后,可以使用此方法验证子树。