XML反序列化和缺少字段-如何知道某物是否丢失? [英] XML deserialization and missing fields - how to know if someting is missing?

查看:187
本文介绍了XML反序列化和缺少字段-如何知道某物是否丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有一些课程,希望将其存储在XML文件中,然后再次阅读.因此,我使用XMLSerialiser,并且工作正常,但是如果有人更改XML文件,我不知道如何处理情况.
例如.我的类Save方法禁止使用此XML文件

Hello,
I have some class and wish to store it in XML file and then read it again. So I use XMLSerialiser, and it works fine, but I don''t know how to handle situation if someone change XML file.
E.g. my class Save method produse this XML file

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FileName>test.txt</FileName>
  <SheetName>List</SheetName>
  <FirstRow>0</FirstRow>
</Data>


如果有人删除其中的所有元素并只留下


if someone delete all elements inside and leave only

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</Data>


我的代码没有收到任何错误或任何异常,但是未初始化XML文件中缺少的属性.
我想了解此类事件,我该如何实现?

代码:


my code don''t get any error or any exception, but leave properties which are missing in XML file uninitialized.
I want to know about such events, how can I achieve this?

Code:

<XmlRootAttribute("Data")> _
Public Class SeriazableClass
#Region "Members"
    Private _FileName As String
    Private _SheetName As String
    Private _FirstRow As Integer
#Region "Properties"
    Property FileName() As String
        Get
            Return _FileName
        End Get
        Set(ByVal value As String)
            _FileName = value
        End Set
    End Property
    Property SheetName() As String
        Get
            Return _SheetName
        End Get
        Set(ByVal value As String)
            _SheetName = value
        End Set
    End Property
    Property FirstRow() As Integer
        Get
            Return _FirstRow
        End Get
        Set(ByVal value As Integer)
            _FirstRow = value
        End Set
    End Property
#End Region
#End Region
#Region "Methods"
    Public Sub New()
    End Sub
    Public Sub Save()
        Dim serializer As New XmlSerializer(GetType(SeriazableClass))
        Dim writer As New StreamWriter("test.xml")
        serializer.Serialize(writer, Me)
        writer.Close()
    End Sub
    Public Shared Function Read() As SeriazableClass
        Dim serializer As New XmlSerializer(GetType(SeriazableClass))
        Try
            Dim reader As New StreamReader("test.xml") ''there is possible file-not-found
            Try
                Read = serializer.Deserialize(reader) ''there is possible invalid-xml
            Finally
                reader.Close()
            End Try
        Catch ex As Exception
            MsgBox(ex.Message & vbCrLf & " during reading", , "Error reading xml file")
            Read = New SeriazableClass
        Finally
        End Try
    End Function
#End Region
End Class

推荐答案

如何定义自己的 XML模式 [ ^ ]并使用xsd.exe [
How about defining your own XML Schema[^] and use xsd.exe[^] to generate your code?

Regards
Espen Harlinn


您无需担心是否丢失了某些内容.如果要将数据序列化为一个对象,则该对象应该知道如何通过应用不会导致对象引发异常的默认值自行纠正错误的数据.
You shouldn''t care if something is missing. If you''re serializing data into an object, that object should know how to correct erroneous data on its own byy applying default values that don''t cause the ob ject to throw an exception.


这篇关于XML反序列化和缺少字段-如何知道某物是否丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆