2

I have created a SSIS package which gets the XML file from a folder and checks with the schema, if the schema fails, the package logs the error and moves the file to a error folder. Currently, I have done all the requirements, and is working fine except the error message i'm getting at the end of the execution.

  1. Validate XML file

enter image description here

  1. The error message which I'm getting

enter image description here

  1. The error message which I'm getting

enter image description here

The package works fine as expected. How can I suppress this error message?

Update #1:

This is my error history

enter image description here

This is my XML Schema validation task properties.

enter image description here

Ranjith Varatharajan
  • 1,596
  • 1
  • 33
  • 76
  • 1
    What is the error message you receive from package execution? I.e. what error message do you see if you check the execution log at Visual Studio? – Ferdipux Jan 11 '18 at 07:30
  • How to supress the error might depend on how severe the error is. You've shown us screenshots of the error happening in Visual studio Once it has run and the error has happened, before you press Stop Debugging, go to the 'Progress' tab and scroll down tofind the error message you're getting. (Note- you could set 'fail package on failure' on the task to 'false' in its properties, but there might be other solutions hence not putting as an answer yet). – Rich Jan 11 '18 at 08:51
  • Thanks for the reply, I have updated the question. – Ranjith Varatharajan Jan 12 '18 at 07:37

1 Answers1

1

Suggestions

The issue may be caused by the FailPackageOnFailure and FailParentOnFailure properties. Click on the Validate XML Task and in the Properties Tab change these properties values. Alos in the Control Flow Go to the properties and change the MaximumErrorCount value and make it bigger than 1.

enter image description here

Also you can find other helpful informations in this link:

Workaround using Script Task

  1. Add 3 Variables to your package:

    @[User::XmlPath] Type: String, Description: Store the Xml File Path
    @[User:XsdPath] Type: String, Description: Store the Xsd File Path
    @[User:IsValidated] Type: Boolean, Description: Store the result of Xml validation
    
  2. Add a script Task, select XmlPath and User:XsdPath as ReadOnly Variables and IsValidated As ReadWrite Variable

  3. Set the Script Language to Visual Basic
  4. In the Script Editor write the following code (this is the whole script task code)

    #Region "Imports"
    Imports System
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Math
    Imports System.Text
    Imports System.Xml
    Imports System.Xml.Schema
    Imports Microsoft.SqlServer.Dts.Runtime
    #End Region
    
    <Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()>
    <System.CLSCompliantAttribute(False)>
    Partial Public Class ScriptMain
        Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    
        Enum ScriptResults
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        End Enum
    
        Public Function LoadXml(xmlFilePath As String, xsdFilePath As String) As Boolean
            Dim settings As New XmlReaderSettings()
            settings.Schemas.Add(Nothing, xsdFilePath)
            settings.ValidationType = ValidationType.Schema
            Dim errorBuilder As New XmlValidationErrorBuilder()
            AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf errorBuilder.ValidationEventHandler)
            Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
            ' Read the document...
            Dim errorsText As String = errorBuilder.GetErrors()
            If errorsText IsNot Nothing Then
                Return False
            Else
                Return True
            End If
        End Function
    
        Public Sub Main()
    
            Dts.Variables("IsValidated").Value = LoadXml(Dts.Variables("XmlPath").Value.ToString, Dts.Variables("XsdPath").Value.ToString)
    
            Dts.TaskResult = ScriptResults.Success
        End Sub
    
    
    End Class
    
    Public Class XmlValidationErrorBuilder
        Private _errors As New List(Of ValidationEventArgs)()
    
        Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
            If args.Severity = XmlSeverityType.Error Then
                _errors.Add(args)
            End If
        End Sub
    
        Public 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
    
  5. Use Precedence Constraints with expression to manipulate both of Validation success and failure cases

enter image description here

enter image description here

Script Code Reference

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • I did all that, It's working fine, the package is not crashing, and tends to work continuously. The only thing is, i'm getting this error message. Since I have handled the xml schema error as shown in the first image I don't want the error message (image 3) after executing the package. That's what I need – Ranjith Varatharajan Jan 12 '18 at 07:33
  • You cannot remove it in this case you can use `ForceExecutionResult` but i doesn't help in this case – Hadi Jan 12 '18 at 07:43
  • You can use a script task to validate Xml, and assign the result to a variable, then use precedence expressions to achieve a similar case – Hadi Jan 12 '18 at 07:45
  • 2
    @RanjithVaradan Also be aware that if you stop showing error messages from tasks, other error will be ignored, which is very critical – Hadi Jan 12 '18 at 08:46
  • 1
    Is it possible to keep the xml schema validation result in a boolean variable and then force this task result to success then process the error file using the boolean variable in the expression? – Ranjith Varatharajan Jan 12 '18 at 09:11
  • I think you need a script task – Hadi Jan 12 '18 at 09:13
  • @RanjithVaradan check my answer update, note that the code i used is copied from the link mentioned in the references, i really couldn't test it , plz give me a reply when testing it – Hadi Jan 12 '18 at 23:09