У меня есть часть программного обеспечения (не мое), которое десериализует файл XML. Если элемент XML не имеет эквивалентного свойства в коде, помеченном <System.Xml.Serialization.XmlElementAttribute(DataType:="dataType")>
, он просто игнорируется, поэтому, если пользователь случайно записал элемент тега в файл XML с опечаткой, он не будет знать, что произошла опечатка, и она была проигнорирована.
Есть ли способ зарегистрировать сообщение, чтобы сообщить пользователю, какие элементы были проигнорированы программным обеспечением?
Например, если XML десериализованный файл имеет этот тег: <test>someValue</test>
он будет игнорироваться и не добавляться в переменную i
, поскольку он не является свойством в коде. Как сообщить пользователю, что он был проигнорирован?
Код десериализации VB:
Dim serializer As New XmlSerializer(GetType(ExportDetailsXml.STATS19ExportData), "http://ipl.com/crash/interfaces")
Dim fs As New FileStream(Filepath, FileMode.Open) ' A FileStream is needed to read the XML document.
Dim reader1 As Xml.XmlReader = Xml.XmlReader.Create(fs)
Dim i As ExportDetailsXml.STATS19ExportData ' Declare an object variable of the type to be deserialized.
i = CType(serializer.Deserialize(reader1), ExportDetailsXml.STATS19ExportData)
XML свойства:
Namespace ExportDetailsXml
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://ipl.com/crash/interfaces"), _
System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://ipl.com/crash/interfaces", IsNullable:=False)> _
Partial Public Class STATS19ExportData
Private exportDetailsField As ExportDetails_Type
Public Property ExportDetails() As ExportDetails_Type
Get
Return Me.exportDetailsField
End Get
Set(value As ExportDetails_Type)
Me.exportDetailsField = value
End Set
End Property
Private sTATS19ExportDataStructureField() As ExportDataStructure
<System.Xml.Serialization.XmlArrayItemAttribute("ExportData", IsNullable:=False)> _
Public Property STATS19ExportDataStructure() As ExportDataStructure()
Get
Return Me.sTATS19ExportDataStructureField
End Get
Set(value As ExportDataStructure())
Me.sTATS19ExportDataStructureField = value
End Set
End Property
End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ipl.com/crash/interfaces")> _
Partial Public Class ExportDetails_Type
Private dateRangeStartField As String
Private dateRangeEndField As String
Private accidentCountField As String
Private vehicleCountField As String
Private casualtyCountField As String
Private districtListField As DistrictList_Type
Public Property DateRangeStart() As String
Get
Return Me.dateRangeStartField
End Get
Set(value As String)
Me.dateRangeStartField = value
End Set
End Property
Public Property DateRangeEnd() As String
Get
Return Me.dateRangeEndField
End Get
Set(value As String)
Me.dateRangeEndField = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute(DataType:="nonNegativeInteger")> _
Public Property AccidentCount() As String
Get
Return Me.accidentCountField
End Get
Set(value As String)
Me.accidentCountField = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute(DataType:="nonNegativeInteger")> _
Public Property VehicleCount() As String
Get
Return Me.vehicleCountField
End Get
Set(value As String)
Me.vehicleCountField = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute(DataType:="nonNegativeInteger")> _
Public Property CasualtyCount() As String
Get
Return Me.casualtyCountField
End Get
Set(value As String)
Me.casualtyCountField = value
End Set
End Property
Public Property DistrictList() As DistrictList_Type
Get
Return Me.districtListField
End Get
Set(value As DistrictList_Type)
Me.districtListField = value
End Set
End Property
End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0"), _
System.SerializableAttribute(), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ipl.com/crash/interfaces")> _
Partial Public Class DistrictList_Type
Private typeField As DistrictExport_Type
Private districtField() As String
Public Property Type() As DistrictExport_Type
Get
Return Me.typeField
End Get
Set(value As DistrictExport_Type)
Me.typeField = value
End Set
End Property
<System.Xml.Serialization.XmlElementAttribute("District")> _
Public Property District() As String()
Get
Return Me.districtField
End Get
Set(value As String())
Me.districtField = value
End Set
End Property
End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0"), _
System.SerializableAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://ipl.com/crash/interfaces")> _
Public Enum DistrictExport_Type
<System.Xml.Serialization.XmlEnumAttribute("All Districts")> _
AllDistricts
<System.Xml.Serialization.XmlEnumAttribute("Filtered Districts")> _
FilteredDistricts
End Enum
etc...