Extensions.Validate 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
验证 、XDocument或XElementXAttribute符合 XSD 中的 XmlSchemaSetXSD。
重载
| 名称 | 说明 |
|---|---|
| Validate(XDocument, XmlSchemaSet, ValidationEventHandler) |
此方法验证是否 XDocument 符合 . 中的 XmlSchemaSetXSD。 |
| Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler) |
此方法验证是否符合 XAttribute 指定的 XmlSchemaObject 和一个 XmlSchemaSet。 |
| Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean) |
XDocument验证符合 XSD 的 XmlSchemaSetXSD(可选)使用架构验证后信息集(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)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
此方法验证是否 XDocument 符合 . 中的 XmlSchemaSetXSD。
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)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
此方法验证是否符合 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。
如果获取某个类型的XmlSchemaObject值XElement,则类型将为 XmlSchemaElement。
如果获取某个类型的XmlSchemaObject值XAttribute,则类型将为 XmlSchemaAttribute。
拥有实例 XmlSchemaObject后,可以使用此方法来验证属性。
适用于
Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
XDocument验证符合 XSD 的 XmlSchemaSetXSD(可选)使用架构验证后信息集(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,该 XSD 定义 Child2 具有 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
addSchemaInfo如果是true,此方法使用架构验证后信息集(PSVI)填充 XML 树。
使用 PSVI 填充 XML 树有两个步骤。
首先,将注释添加到树中的所有节点,以便调用或Extensions.GetSchemaInfo调用Extensions.GetSchemaInfo树中的元素或属性。
其次,XSD 中定义的默认元素和属性将添加到 XML 树中。 通过调用其中 GetSchemaInfo 一种方法,可以确定是否从 XSD 添加特定元素或属性作为默认元素或属性。
适用于
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
此方法验证子树是否符合 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。
如果获取某个类型的XmlSchemaObject值XElement,则类型将为 XmlSchemaElement。
如果获取某个类型的XmlSchemaObject值XAttribute,则类型将为 XmlSchemaAttribute。
获得实例 XmlSchemaObject后,可以使用此方法来验证子树。
适用于
Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
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 架构。 修改属性后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只验证属性需要更少的处理时间。
addSchemaInfo如果是true,此方法使用架构验证后信息集(PSVI)填充属性。 使用 PSVI 填充 XML 树后,可以调用 Extensions.GetSchemaInfo 已验证的属性。 如果要编写依赖于返回 GetSchemaInfo的数据的代码,这非常有用。
如果传递 null , validationEventHandler此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证属性,请使用 . 的 XmlSchemaObject实例。 可以通过多种方式获取此实例。 一种简单方法如下:
验证文档是否符合架构。
通过调用 Validate 扩展方法添加架构验证后信息集 (PSVI)。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索到的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的XmlSchemaObject值XElement,则类型将为 XmlSchemaElement。
如果获取某个类型的XmlSchemaObject值XAttribute,则类型将为 XmlSchemaAttribute。
拥有实例 XmlSchemaObject后,可以使用此方法来验证属性。
适用于
Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean)
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
- Source:
- XNodeValidator.cs
验证子树是否符合 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 树的根目录)是否符合架构。 修改子树后,通常使用此方法,并且希望确保它仍符合其架构。 可以验证整个文档,但只需花费更少的处理时间来验证子树。
addSchemaInfo如果是true,则此方法使用架构验证后信息集(PSVI)填充 XML 树。
使用 PSVI 填充 XML 树有两个方面。
首先,将注释添加到树中的所有节点,以便现在可以调用 GetSchemaInfo 树中的元素或属性。
其次,XSD 中定义的默认元素和属性将添加到 XML 树中。 通过调用其中 GetSchemaInfo 一种方法,可以确定是否从 XSD 添加特定元素或属性作为默认元素或属性。
如果传递 null , validationEventHandler此方法会在验证错误时引发异常。 验证警告不会引发异常。
若要验证子树,请使用 . 的 XmlSchemaObject实例。 可以通过多种方式获取此实例。 一种简单方法如下:
验证文档是否符合架构。
通过调用 Validate 扩展方法添加架构验证后信息集 (PSVI)。
GetSchemaInfo调用扩展方法以检索实现IXmlSchemaInfo的对象。 从检索到的对象中,可以获取一个 XmlSchemaObject。
如果获取某个类型的XmlSchemaObject值XElement,则类型将为 XmlSchemaElement。
如果获取某个类型的XmlSchemaObject值XAttribute,则类型将为 XmlSchemaAttribute。
拥有子树的 XmlSchemaObject实例后,可以使用此方法来验证子树。