0

i need this to automatically vallidate the xml file. no need to verify it. so how?

Dim document As XDocument = XDocument.Load("C:\Purchase Request Setup\Crystal reports\crptPurchaseRequest.xml")

is this how to read xml?

or

 dt.ReadXml("C:\Purchase Request Setup\Crystal reports\crptPurchaseRequest.xml")
  • Validate the XML against what? Do you want to ensure that the XML is valid XML or do you want to validate the file against an XSD? – IronAces Sep 08 '17 at 07:36

1 Answers1

0

There are several ways to validate XML...

Public Shared Function IsValidXml(xmlString As String) As Boolean
    Dim tagsWithData As New Regex("<\w+>[^<]+</\w+>")

    If String.IsNullOrEmpty(xmlString) OrElse tagsWithData.IsMatch(xmlString) = False Then
        Return False
    End If

    Try
        Dim xmlDocument As New XmlDocument()
        xmlDocument.LoadXml(xmlString)
        Return True
    Catch xmlException As XmlException
        Return False
    End Try
End Function

N.B. Taken from here

Or you could simply handle the exception

Try
    Dim document As XDocument = XDocument.Load("C:\Purchase Request Setup\Crystal reports\crptPurchaseRequest.xml")
Catch ex As XmlException 'Handle the exception
    'Probably poorly formed XML...
End Try
IronAces
  • 1,857
  • 1
  • 27
  • 36