No Validation errors using XDocument.Validate

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I am trying to validate an XML file against a set of XSD files but get no errors even when I intentionally change my XML to have errors, like entering special char when the XSD declares the type to be String, and still it validates correctly.
Need help. Below is my code. I have 4 schemas.

Imports System.Xml.Linq<br/>
Imports System.Xml<br/>
Imports System.Xml.Schema
Imports <xmlns:ns=" http://www.test.gov/trip http://www.test.gov/trip
<br/>
Module XMLTest<br/>
Dim errors As Boolean = False
Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)<br/>
Console.WriteLine("{0}", e.Message)<br/>
errors = True<br/>
End Sub
Sub main()<br/>
Dim doc As XDocument = _<br/>
<?xml version="1.0" encoding="UTF-8"?><br/>
<TRIPFileMaintanance xsi:schemaLocation=" http://www.test.gov/trip http://www.test.gov/trip Maintenance.xsd Header.xsd MasterFile.xsd efileTypes.xsd" xmlns:ns=" http://www.test.gov/trip http://www.test.gov/trip "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance <br/>
<TRIPHeader><br/>
<FileCreationTimestamp>2011-01-31</FileCreationTimestamp><br/>
<TransmitterName>Big State Age</TransmitterName><br/>
<SubmissionID1>676</SubmissionID1><br/>
<ProcessType>Test</ProcessType><br/>
</TRIPHeader><br/>
<DebtRecord><br/>
<TypeTransaction>New</TypeTransaction><br/>
<Individual><br/>
<SSN>5675675</SSN><br/>
<TaxpayerName><br/>
<FirstName></FirstName><br/>
<MiddleInitial>A</MiddleInitial><br/>
<LastName>Doe</LastName><br/>
<NameSuffix>JR</NameSuffix><br/>
</TaxpayerName><br/>
</Individual><br/>
<AgencyDebtorAddress><br/>
<AddressLine1>123 Main st</AddressLine1><br/>
<City>Madison</City><br/>
<State>WI</State><br/>
<ZipCode>53708</ZipCode><br/>
</AgencyDebtorAddress><br/>
<AgencyInternalDebtNumber>5654654</AgencyInternalDebtNumber><br/>
<AgencyDebtBalance>tyut</AgencyDebtBalance><br/>
<AgencyNumber>555</AgencyNumber><br/>
<AgencySubunitNumber>444</AgencySubunitNumber><br/>
</DebtRecord><br/>
<DebtRecord><br/>
<TypeTransaction>New1</TypeTransaction><br/>
<Individual><br/>
<SSN>67575675</SSN><br/>
<TaxpayerName><br/>
<FirstName>12345678901234567890233</FirstName><br/>
<MiddleInitial>A1</MiddleInitial><br/>
<LastName>Doe1</LastName><br/>
<NameSuffix>JR1</NameSuffix><br/>
</TaxpayerName><br/>
</Individual><br/>
<AgencyDebtorAddress><br/>
<AddressLine1>456 Main st</AddressLine1><br/>
<City>Milwaukee</City><br/>
<State>WI</State><br/>
<ZipCode>53708</ZipCode><br/>
</AgencyDebtorAddress><br/>
<AgencyInternalDebtNumber>76865</AgencyInternalDebtNumber><br/>
<AgencyDebtBalance>200.00</AgencyDebtBalance><br/>
<AgencyNumber>666</AgencyNumber><br/>
<AgencySubunitNumber>777</AgencySubunitNumber><br/>
</DebtRecord><br/>
</TRIPFileMaintanance>
doc.Save("test.xml")<br/>
Dim xdoc As XDocument = XDocument.Load("test.xml")
Dim schemas As XmlSchemaSet = New XmlSchemaSet()<br/>
Dim xsdMarkup As XElement = XElement.Load("Maintenance.xsd")
<br/>
schemas.Add(Nothing, xsdMarkup.CreateReader)<br/>
xdoc.Validate(schemas, AddressOf XSDErrors, True)<br/>
Console.WriteLine("doc {0}", IIf(errors = True, "did not validate", "validated"))<br/>
End Sub<br/>
End Module

View the full article
 
Back
Top