I'm new to Schema validation and I'm having a code which performs schema validation using "ValidationType.Schema".
There are a few tags which are optional in my output XML file and to validate this I've set the corresponding XSD file tags as "MinOccurs = 0", however I'm still getting the schema validation failed errors.
I'm not able to figure out if the SEQUENCE in which the tags are mentioned in the XSD also matter with the SEQUENCE of the tags in XML file or does this only validate the existance.
Ex:
CASE 1:
---OUTPUT XML---
<tag1>1</tag1>
<tag2>2</tag2>
----XSD File
<tag1>
<tag2>
CASE 2:
---OUTPUT XML---
<tag2>1</tag2>
<tag1>2</tag1>
----XSD File
<tag1>
<tag2>
which one of these will fail? if any?
I'm using the validation type as "ValidationType.Schema"
Thanks for your help.
Asked
Active
Viewed 226 times
0

Amar
- 303
- 1
- 3
- 19
-
possible duplicate of http://stackoverflow.com/questions/7448616/why-is-node-order-important-in-xml – Anil Dec 08 '14 at 05:19
1 Answers
0
@user1705851, node order matters, as element can have same name, but you can avoid this by using all atribute in xsd. Working code of your nodes given below.
Imports System.Xml.Schema
Module Module1
Dim errors As Boolean = False
Sub Main()
Dim xsdMarkup As XElement = _
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<xsd:element name='Root'>
<xsd:complexType>
<xsd:all>
<xsd:element name='tag1' minOccurs='0' maxOccurs='1'/>
<xsd:element name='tag2' minOccurs='0' maxOccurs='1'/>
</xsd:all>
</xsd:complexType>
</xsd:element>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("", xsdMarkup.CreateReader)
Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<tag1>1</tag1>
<tag2>2</tag2>
</Root>
Dim doc2 As XDocument = _
<?xml version='1.0'?>
<Root>
<tag2>1</tag2>
<tag1>2</tag1>
</Root>
Console.WriteLine("Validating doc1")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors)
Console.WriteLine("doc1 {0}", IIf(errors = True, "did not validate", "validated"))
Console.WriteLine()
Console.WriteLine("Validating doc2")
errors = False
doc2.Validate(schemas, AddressOf XSDErrors)
Console.WriteLine("doc2 {0}", IIf(errors = True, "did not validate", "validated"))
Console.ReadLine()
End Sub
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
End Module

Anil
- 3,722
- 2
- 24
- 49