This has been driving me mad all day, thank goodness its hometime.
Basically I have a routine that validates an XML document, here is the code:
Here is the XSD (AddEmployee.XSD):
Finally here is my XML document;
If you look at the XSD file youll see that the element ID is supposed to mandatory in that I have set minoccurs to 1. However when I run the code this doesnt seem to be flagged as an error. Also I get 20 errors like Could not find schema information for the element Employees etc.
Can someone show me what stupid mistake I have made as I am totally stumped!
Thanks, Dave.
Basically I have a routine that validates an XML document, here is the code:
Code:
Private Function ValidateXML(ByRef xmlDoc As XmlDocument, ByVal streamReader As StreamReader) As Boolean
Dim buffer() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(xmlDoc.InnerXml)
Dim memstrm As New System.IO.MemoryStream(buffer)
Dim xmlReader As New XmlTextReader(memstrm)
memstrm.Close()
Dim xmlVal As New XmlValidatingReader(xmlReader)
xmlVal.Schemas.Add("urn:pleaseWork", Server.MapPath("XSD/AddEmployee.xsd"))
xmlVal.ValidationType = ValidationType.Schema
AddHandler xmlVal.ValidationEventHandler, AddressOf SchemaValidationEventHandler
Try
Dim isValid As Boolean = True
While xmlVal.Read
Dim strr As String = xmlVal.Value
End While
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Sub SchemaValidationEventHandler(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
If e.Severity = XmlSeverityType.Error Then
Dim poo As String = String.Empty
_errorCount += 1
ElseIf e.Severity = XmlSeverityType.Warning Then
Dim notAsPoo As String = String.Empty
_errorCount += 1
End If
End Sub
Here is the XSD (AddEmployee.XSD):
Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="AddEmployee" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:pleaseWork" elementFormDefault="qualified" targetNamespace="urn:pleaseWork">
<xs:element name="Employees">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="1"/>
<xs:element name="Name" type="xs:string" minOccurs="1"/>
<xs:element name="ActivityID" type="xs:int" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Finally here is my XML document;
Code:
<?xml version="1.0" standalone="yes"?>
<Employees>
<Employee>
<Name>Dave Jones</Name>
<ActivityID>9</ActivityID>
</Employee>
<Employee>
<ID>111</ID>
<Name>Wayne Wallice</Name>
<ActivityID>1</ActivityID>
</Employee>
<Employee>
<ID>1111</ID>
<Name>Justin Davies</Name>
<ActivityID>2</ActivityID>
</Employee>
<Employee>
<ID>11111</ID>
<Name>Matthew Jones</Name>
<ActivityID>0</ActivityID>
</Employee>
<Employee>
<ID>111111</ID>
<Name>Steve Pear</Name>
<ActivityID>2</ActivityID>
</Employee>
</Employees>
If you look at the XSD file youll see that the element ID is supposed to mandatory in that I have set minoccurs to 1. However when I run the code this doesnt seem to be flagged as an error. Also I get 20 errors like Could not find schema information for the element Employees etc.
Can someone show me what stupid mistake I have made as I am totally stumped!
Thanks, Dave.