I have a VB.NET Function that Validates my XML vs a schema and then if it fails outputs the failure to an XML response for the Web Service.
The issue I have is when I said correctly formatted XML it validates and is successful, then if I send incorrect XML the validator correct states where it is failing in validation.
If I THEN send the correct XML that is formatted correctly through the Web Service the my Validation Check function returns the same error that it experienced when checking the previous XML, almost as if its 'caching' the issue.
Can anyone tell me why this is the case, I can confirm the XML being passed through the validation is 100% correct as I've viewed it and exported to another program of mine outside of VB.NET that validates the XML.
My ValidateXML Function
Public Shared Function ValidateXML(xmlFilePath As String) As String
Dim doc As New XmlDocument()
doc.LoadXml(xmlFilePath)
doc.Schemas.Add(Nothing, "http://www.fresh.co.uk/freshit/fresh_lead_schema.xsd")
Dim errorBuilder As New XmlValidationErrorBuilder()
doc.Validate(New ValidationEventHandler(AddressOf XmlValidationErrorBuilder.ValidationEventHandler))
FIGCloud.errorsText = XmlValidationErrorBuilder.GetErrors()
If FIGCloud.errorsText IsNot Nothing Then
'Throw New Exception(errorsText)
Return "Failed"
Else
Dim ReturnText As String = doc.InnerXml 'Returns XML Document as a String
Return ReturnText
End If
End Function
My XMLvalidationErrorBuilder Class I took from another source
Public Class XmlValidationErrorBuilder
Public Shared _errors As New List(Of ValidationEventArgs)()
Public Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Error Then
_errors.Add(args)
End If
End Sub
Public Shared Function GetErrors() As String
If _errors.Count <> 0 Then
Dim builder As New StringBuilder()
builder.Append("The following ")
builder.Append(_errors.Count.ToString())
builder.AppendLine(" error(s) were found while validating the XML document against the XSD:")
For Each i As ValidationEventArgs In _errors
builder.Append("* ")
builder.AppendLine(i.Message)
Next
Return builder.ToString()
Else
Return Nothing
End If
End Function
End Class
Here is the correct formatted XML
<FreshLead>
<ContactDetails>
<Title>Mr</Title>
<FirstName>Joe</FirstName>
<LastName>Bloggs</LastName>
<EmailAddress>joe.bloggs@davisco.co.uk</EmailAddress>
<HomeTel>01527 321 850</HomeTel>
<MobileTel>07771111111</MobileTel>
<DateOfBirth>21/05/1987</DateOfBirth>
<Address>Joe Bloggs Road, Here</Address>
<Postcode>B98 9PA</Postcode>
</ContactDetails>
<TradeDetails>
<TradingName>Motor Trade Direct</TradingName>
<TypeOfBus>Sales</TypeOfBus>
<AgeOfBus>5 Years</AgeOfBus>
<NoOfEmployees>5</NoOfEmployees>
<NoOfDrivers>5</NoOfDrivers>
<FullPartTime>Full-Time</FullPartTime>
<BusPostcode>B98 9PA</BusPostcode>
<BusPremises>No</BusPremises>
<DemoCover>No</DemoCover>
<PremIndem>10000</PremIndem>
<RoadRisksIndem>20000</RoadRisksIndem>
<VolXS>250</VolXS>
<CoverStart>19/03/2015</CoverStart>
<CoverReq>Comprehensive</CoverReq>
<MTExperience>5 Years</MTExperience>
</TradeDetails>
<ProposerDetails>
<UKResident>5 Years</UKResident>
<FullUKLic>10 Years+</FullUKLic>
<MT.NCB>2 Years</MT.NCB>
<PC.NCB>2 Years</PC.NCB>
<ClaimsConv5yr>No</ClaimsConv5yr>
</ProposerDetails>
<AddDetails>
<CurrInsurer>Markerstudy</CurrInsurer>
<BestQuote>2000.00</BestQuote>
<ContactTime>Evening</ContactTime>
<PrefContact>Email</PrefContact>
</AddDetails>
This validates correctly when sending through the Web Service the first time.
I have just changed 2 of the fields so that they contain 'Strings' within 'Int16' fields and this then fails (as it should) these fields are the 'Indem' fields.
<FreshLead>
<ContactDetails>
<Title>Mr</Title>
<FirstName>Joe</FirstName>
<LastName>Bloggs</LastName>
<EmailAddress>joe.bloggs@davisco.co.uk</EmailAddress>
<HomeTel>01527 321 850</HomeTel>
<MobileTel>07771111111</MobileTel>
<DateOfBirth>21/05/1987</DateOfBirth>
<Address>Joe Bloggs Road, Here</Address>
<Postcode>B98 9PA</Postcode>
</ContactDetails>
<TradeDetails>
<TradingName>Motor Trade Direct</TradingName>
<TypeOfBus>Sales</TypeOfBus>
<AgeOfBus>5 Years</AgeOfBus>
<NoOfEmployees>5</NoOfEmployees>
<NoOfDrivers>5</NoOfDrivers>
<FullPartTime>Full-Time</FullPartTime>
<BusPostcode>B98 9PA</BusPostcode>
<BusPremises>No</BusPremises>
<DemoCover>No</DemoCover>
<PremIndem>Over 10000</PremIndem>
<RoadRisksIndem>Over 20000</RoadRisksIndem>
<VolXS>250</VolXS>
<CoverStart>19/03/2015</CoverStart>
<CoverReq>Comprehensive</CoverReq>
<MTExperience>5 Years</MTExperience>
</TradeDetails>
<ProposerDetails>
<UKResident>5 Years</UKResident>
<FullUKLic>10 Years+</FullUKLic>
<MT.NCB>2 Years</MT.NCB>
<PC.NCB>2 Years</PC.NCB>
<ClaimsConv5yr>No</ClaimsConv5yr>
</ProposerDetails>
<AddDetails>
<CurrInsurer>Markerstudy</CurrInsurer>
<BestQuote>2000.00</BestQuote>
<ContactTime>Evening</ContactTime>
<PrefContact>Email</PrefContact>
</AddDetails>
But when I pass the first working example back through it fails with the same errors as the above XML, I have provided the location of the schema it validates against in my code, for anyone that wishes to use a working example and figure out where I'm going wrong.
Thanks