I have a program I built a while back to monitor a list of folders the user (me in this case) defines in a UI. It emails a list of users if a file was added or changed since the last check. It’s been working for years. Just today I started getting an error about there being an error in the XML. I looked at it and nothing was missing or incorrect, so I tried clearing & reentering the inputs. Still not working.
Here’s the XML, as it stands after that failed attempt to re-enter the actual folders to watch. I tried loading the XML to the program with it like this and still got the error, so it’s something about this header. I’m probably just missing something obvious.
<DataTable>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Watched_x0020_Folders" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Watched_x0020_Folders">
<xs:complexType>
<xs:sequence>
<xs:element name="Folder_To_Watch" type="xs:string" minOccurs="0" />
<xs:element name="Include_Sub" type="xs:string" minOccurs="0" />
<xs:element name="File_Ext" type="xs:string" minOccurs="0" />
<xs:element name="Folder_Emails" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" />
</DataTable>
The error I get is below (everything after “Error Details” are the actual error message & inner message). NOTE: I get the same error when attempting to serialize as well.
9/23/2024 2:02:28 PM – Encountered an error while performing the deserialization for Load_From_XML. Error Details: There is an error in XML document (3, 4).. Inner Exception: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
I found this similar issue, but I’m not sure how to understand the “row” & “column” designator when I get the error without any elements in the XML. Other XML Error question
For context the functions I am using to serialize & deserialize are utilized in another program of mine that is working fine still, so it’s highly unlikely to be a fault there. Adding the serialize & deserialize functions for context though:
Public Function ToXML(ByVal o As Object, ByVal FName As String, ByVal path As String, ByVal spath As String) As String
'
Dim t As String = ""
Dim xs As New System.Xml.Serialization.XmlSerializer(o.GetType)
Dim ret As String = ""
Dim ie_msg As String = ""
Try
'check that the folder path exists
If Not System.IO.Directory.Exists(path.Substring(0, spath.LastIndexOf(""))) Then
System.IO.Directory.CreateDirectory(spath.Substring(0, spath.LastIndexOf("")))
End If
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
ret = "Encountered an error while creating the neccesary file path. Please address the following: " & ex.Message & ie_msg
End Try
'Check if the file exists
If System.IO.File.Exists(spath) Then
Try
'Delete the existing file
System.IO.File.Delete(spath)
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
EL.AddErr("Encountered an error while attempting to clear the current information from the existing version of the XML file for " & FName &
". Error details: " & ex.Message & ie_msg, path)
ret = "failed to clear old XML data from the identified file"
Return ret
Exit Function
End Try
End If
'Now start writing the XML file
Try
Using xsw As New System.IO.StreamWriter(spath, True)
xs.Serialize(xsw, o)
End Using
'Next
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
EL.AddErr("Encountered an error while serializing the information for " & FName & ". Error Details: " & ex.Message & ie_msg, path)
ret = "failed to finish serializing the provided data"
End Try
Return ret
End Function
''' <summary>
''' Converts the XML found in the identified document into data populated on the provided object
''' </summary>
''' <param name="o">The object to be loaded with data from the XML document</param>
''' <param name="spath">The full file path to the XML document to deserialize</param>
''' <param name="FName">The name of the function requesting the deserialization</param>
''' <param name="path">The full file path to log any errors to</param>
''' <returns>A string detailing any errors that occur during the deserialization (empty on success)</returns>
''' <remarks></remarks>
Public Function FromXML(ByRef o As Object, ByVal spath As String, ByVal FName As String, ByVal path As String) As String
'
Dim ret As String = ""
Dim src As New Xml.XmlDocument
Dim xs As System.Xml.Serialization.XmlSerializer
Dim ie_msg As String = ""
Try
'Setup the serializer based on the passed in object type
xs = New System.Xml.Serialization.XmlSerializer(o.GetType)
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
EL.AddErr("Encountered an error while trying to setup the XML deserializer to handle creating object's of the identified type for " & FName &
". Error Details: " & ex.Message & ie_msg, path)
ret = "serializer setup failing for the identified object"
Return ret
Exit Function
End Try
Try
'Load the document
src.Load(spath)
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
EL.AddErr("Encountered an error loading the source XML file identified by " & FName & ". Error Details: " & ex.Message & ie_msg, path)
ret = "failure to load the source XML document"
Return ret
Exit Function
End Try
Try
'Deserialize and load the object
Using xsw As New System.IO.StreamReader(spath)
o = xs.Deserialize(xsw)
End Using
Catch ex As Exception
If Not IsNothing(ex.InnerException.Message) Then
ie_msg = " Inner Exception: " & ex.InnerException.Message
End If 'else nothing to capture
EL.AddErr("Encountered an error while performing the deserialization for " & FName & ". Error Details: " & ex.Message & ie_msg, path)
ret = "failure during actual deserialization"
End Try
Return ret
End Function
11