I have a schema which validates my xml file but I want to make further validations such as numeric ranges, birthday structure eg. DD/MM/YY not mm/dd/yy. student name allows for special characters eg. _ in name etc.
at the moment when I run my code I get the error:
[Error]: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e)
sample of my xml:
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<studentTable xmlns="namespace">
<student>
<ID>0</ID>
<student_name>John</student_name>
<birthday>25/09/1997</birthday>
</student>
I have tried the following code but recieve an error " Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e)"
Dim xdoc As XmlDocument
Dim nodelist As XmlNodeList
Dim node As XmlNode
Dim ID, birthday, student_name As String
xdoc = New XmlDocument
xdoc.LoadXml("student2.xml")
nodelist = xdoc.SelectNodes("/studentTable/student")
For Each node In nodelist
ID = node.ChildNodes.Item(0).Attributes.GetNamedItem("ID").Value
birthday = node.ChildNodes.Item(1).Attributes.GetNamedItem("birthday").Value
student_name = node.ChildNodes.Item(2).Attributes.GetNamedItem("student_name").Value
Dim rgx As New Regex("^[0-9]*$")
If rgx.IsMatch(ID) = False Then
lstErrs.Add("Invalid ID number")
End If
Dim reg As New Regex("^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$")
If reg.IsMatch(birthday) = False Then
lstErrs.Add("Invalid birthday")
End If
Dim regx As New Regex("^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$")
If regx.IsMatch(student_name) = False Then
lstErrs.Add("Invalid Name")
End If
Next
xml errors
If lstErrs.Count > 0 Then
'-- Output list of errors
MsgBox("Complete but with errors! Check error file.") '& vbCrLf & vbCrLf & Strings.Join(lstErrs.ToArray, vbCrLf))
fileWriter.WriteLine("Filename: " & strFilNme)
fileWriter.WriteLine(vbCrLf)
fileWriter.WriteLine("Errors:")
For i As Integer = 0 To lstErrs.Count - 1
fileWriter.WriteLine(lstErrs(i))
Next
Else
MsgBox("Complete!")
Exit Sub
End If
fileWriter.Close()
Catch ex As XmlSchemaValidationException
MsgBox("Complete but with errors! Check error file.")
fileWriter.WriteLine("[Error]: XmlSchemaValidationException -error!!!!!!")
fileWriter.WriteLine("LineNumber = {0}", ex.LineNumber)
fileWriter.WriteLine("LinePosition = {0}", ex.LinePosition)
fileWriter.WriteLine("Message = {0}", ex.Message)
fileWriter.WriteLine("Source = {0}", ex.Source)
Catch exOther As Exception
MsgBox("Complete but with errors! Check error file.")
fileWriter.WriteLine("[Error]: " & exOther.Message & exOther.StackTrace)
Finally
If Not IsNothing(reader) Then
reader.Close()
End If
If Not IsNothing(fileWriter) Then
fileWriter.Close()
End If
End Try
End Sub
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
'MsgBox("Display Errors")
Select Case e.Severity
Case XmlSeverityType.Error
lstErrs.Add("Error: {0} " & e.Message)
Case XmlSeverityType.Warning
lstErrs.Add("Warning {0} " & e.Message)
Case Else
lstErrs.Add(e.Message)
End Select
End Sub
I have tried changing LoadXml to just load but then my code runs without an error but my regex doesnt validate the xml values. any help would be great thanks.