是否有更快的方法来存储/读取XML文档中的数据?

问题描述 投票:0回答:1

我一直在VB.NET中开发Windows窗体应用程序,以解析大型的XML文档,该文档是日英字典。我最初构建了一个基于字符串的解析器,该解析器读取所有XML标记并手动提取其内容。完全解析XML文件平均需要30秒钟。此时,我还没有听说过反序列化。

然后,感谢severaluserson this site,我可以正常工作的XML反序列化程序将XML文件中的所有数据加载到类对象中。辉煌。但是平均需要20秒才能加载。

虽然效率提高了33%,但我想知道是否有我不知道或没有考虑过的更好的方法。或者也许是我现有的代码/类定义的效率低下,我至少可以减少它。

该文件长470万行,显示超过180,000个字典条目的信息,因此我准备接受可能没有更快速的XML解析方法。我想知道是否有其他替代方法来存储数据,而不是XML文本文件格式,Windows窗体应用程序可以更快地读取它。

这是我当前用于反序列化的代码:

Sub Deserialise()
    Dim settings As XmlReaderSettings = New XmlReaderSettings
    settings.DtdProcessing = DtdProcessing.Parse

    Dim xmlPath As String = Path.Combine(Application.StartupPath, jmdictpath)

    Dim serialiser As New XmlSerializer(GetType(JMdict))

    Using rdr As Xml.XmlReader = Xml.XmlReader.Create(xmlPath, settings)
        dict = CType(serialiser.Deserialize(rdr), JMdict)
    End Using
End Sub

这是类定义:

<XmlRoot>
Public Class JMdict
    <XmlElement("entry")>
    Public Property entrylist As List(Of Entry)
End Class

<Serializable()>
<XmlType("entry")>
Public Class Entry
    <XmlElement("ent_seq")>
    Public Property EntrySequence As Integer
    <XmlElement("k_ele")>
    Public Property Keywords As List(Of KeywordElement)
    <XmlElement("r_ele")>
    Public Property Readings As List(Of ReadingElement)
    <XmlElement("sense")>
    Public Property Senses As List(Of SenseElement)
End Class

<Serializable()>
<XmlType("k_ele")>
Public Class KeywordElement
    <XmlElement("keb")>
    Public Property Keyword As String
    <XmlElement("ke_pri")>
    Public Property KeywordPriority As List(Of String)
    <XmlElement("ke_inf")>
    Public Property KeywordOrthography As List(Of String)
End Class

<Serializable()>
<XmlType("r_ele")>
Public Class ReadingElement

    <XmlElement("reb")>
    Public Property Reading As String
    <XmlElement("re_pri")>
    Public Property ReadingPriority As List(Of String)
    <XmlElement("re_inf")>
    Public Property ReadingOrthography As List(Of String)
    <XmlElement("re_restr")>
    Public Property ReadingToKeywordRestriction As List(Of String)

    Private _NotTrueReading As Boolean

    <XmlElement("re_nokanji")>
    Public Property NotTrueReading As String
        Get
            Return _NotTrueReading
        End Get
        Set
            If NotTrueReading IsNot Nothing Then
                _NotTrueReading = True
            Else
                _NotTrueReading = False
            End If
        End Set
    End Property
End Class

<Serializable()>
<XmlType("sense")>
Public Class SenseElement
    <XmlElement("pos")>
    Public Property PartOfSpeech As List(Of String)
    <XmlElement("gloss")>
    Public Property Gloss As List(Of GlossElement)

    <XmlElement("stagk")>
    Public Property SenseRestrictedToKeyword As List(Of String)
    <XmlElement("stagr")>
    Public Property SenseRestrictedToReading As List(Of String)

    <XmlElement("xref")>
    Public Property CrossReference As List(Of String)
    <XmlElement("ant")>
    Public Property Antonym As List(Of String)
    <XmlElement("field")>
    Public Property Field As List(Of String)
    <XmlElement("dial")>
    Public Property Dialect As List(Of String)
    <XmlElement("s_inf")>
    Public Property SenseInformation As List(Of String)
    <XmlElement("misc")>
    Public Property Misc As List(Of String)
    <XmlElement("lsource")>
    Public Property LanguageSource As List(Of LanguageSourceElement)
End Class


<Serializable()>
<XmlType("gloss")>
Public Class GlossElement
    <XmlAttribute("xml:lang")>
    Public Property Language As String
    <XmlAttribute("g_type")>
    Public Property GlossType As String
    <XmlText>
    Public Property Text As String
    Public Overrides Function ToString() As String
        Return Text
    End Function
End Class

<Serializable()>
<XmlType("lsource")>
Public Class LanguageSourceElement
    <XmlAttribute("xml:lang")>
    Public Property Language As String
    <XmlAttribute("ls_type")>
    Public Property LanguageSourceType As String

    Private _IsWaseieigo As Boolean
    <XmlAttribute("ls_wasei")>
    Public Property IsWaseieigo As String
        Get
            Return _IsWaseieigo
        End Get
        Set
            If IsWaseieigo = "y" Then
                _IsWaseieigo = True
            Else
                _IsWaseieigo = False
            End If
            '_IsWaseieigo = Value
        End Set
    End Property

    <XmlText>
    Public Property Text As String
    Public Overrides Function ToString() As String
        Return Text
    End Function

End Class

我们怎么看?是否有希望有更快的解决方案?

xml vb.net xml-parsing xml-deserialization
1个回答
0
投票

尝试xml linq。由于您未发布任何xml,因此未测试以下代码。 :

Imports System.Xml
Imports System.Xml.Linq
Module Module1
    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim JMdict As JMdict = JMdict.Parse(FILENAME)
    End Sub

    Public Class JMdict
        Public Property entrylist As List(Of Entry)

        Public Shared Function Parse(filename As String) As JMdict
            Dim doc As XDocument = XDocument.Parse(filename)
            Dim newJMdict As New JMdict()
            Dim jmDict = doc.Root

            newJMdict.entrylist = jmDict.Elements("entry").Select(Function(x) Entry.Parse(x))
            Return newJMdict
        End Function
    End Class

    Public Class Entry
        Public Property EntrySequence As Integer
        Public Property Keywords As List(Of KeywordElement)
        Public Property Readings As List(Of ReadingElement)
        Public Property Senses As List(Of SenseElement)

        Public Shared Function Parse(xEntry As XElement) As Entry
            Dim newEntry As New Entry()
            newEntry.EntrySequence = CType(xEntry.Element("ent_seq"), Integer)
            newEntry.Keywords = xEntry.Elements("k_ele").Select(Function(x) KeywordElement.Parse(x))

            newEntry.Readings = xEntry.Elements("r_ele").Select(Function(x) ReadingElement.Parse(x))
            newEntry.Senses = xEntry.Elements("sense").Select(Function(x) SenseElement.Parse(x))


            Return newEntry
        End Function
    End Class

    Public Class KeywordElement
        Public Property Keyword As String
        Public Property KeywordPriority As List(Of String)
        Public Property KeywordOrthography As List(Of String)

        Public Shared Function Parse(xKeywordElement As XElement) As KeywordElement
            Dim newKeywordElement As New KeywordElement()
            newKeywordElement.Keyword = CType(xKeywordElement.Element("key"), String)
            newKeywordElement.KeywordPriority = xKeywordElement.Elements("ke_pri").Select(Function(x) CType(x, String))
            newKeywordElement.KeywordOrthography = xKeywordElement.Elements("ke_inf").Select(Function(x) CType(x, String))

            Return newKeywordElement
        End Function


    End Class

    Public Class ReadingElement

        Public Property Reading As String
        Public Property ReadingPriority As List(Of String)
        Public Property ReadingOrthography As List(Of String)
        Public Property ReadingToKeywordRestriction As List(Of String)

        Private _NotTrueReading As Boolean

        Public Property NotTrueReading As String
            Get
                Return _NotTrueReading
            End Get
            Set(value As String)
                If NotTrueReading IsNot Nothing Then
                    _NotTrueReading = True
                Else
                    _NotTrueReading = False
                End If
            End Set
        End Property

        Public Shared Function Parse(xReadingElement As XElement) As ReadingElement
            Dim newReadingElement As New ReadingElement
            newReadingElement.Reading = CType(xReadingElement.Element("reb"), String)
            newReadingElement.ReadingPriority = xReadingElement.Elements("re_pri").Select(Function(x) CType(x, String))
            newReadingElement.ReadingOrthography = xReadingElement.Elements("re_inf").Select(Function(x) CType(x, String))
            newReadingElement.ReadingToKeywordRestriction = xReadingElement.Elements("re_restr").Select(Function(x) CType(x, String))

            newReadingElement.NotTrueReading = CType(xReadingElement.Element("re_nokanji"), String)

            Return newReadingElement
        End Function
    End Class

    Public Class SenseElement
        Public Property PartOfSpeech As List(Of String)
        Public Property Gloss As List(Of GlossElement)

        Public Property SenseRestrictedToKeyword As List(Of String)
        Public Property SenseRestrictedToReading As List(Of String)
        Public Property CrossReference As List(Of String)
        Public Property Antonym As List(Of String)
        Public Property Field As List(Of String)
        Public Property Dialect As List(Of String)
        Public Property SenseInformation As List(Of String)
        Public Property Misc As List(Of String)
        Public Property LanguageSource As List(Of LanguageSourceElement)

        Public Shared Function Parse(xSenseElement As XElement) As SenseElement
            Dim newSenseElement As New SenseElement
            newSenseElement.PartOfSpeech = xSenseElement.Elements("pos").Select(Function(x) CType(x, String))
            newSenseElement.Gloss = xSenseElement.Elements("gloss").Select(Function(x) GlossElement.Parse(x))
            newSenseElement.SenseRestrictedToKeyword = xSenseElement.Elements("stagk").Select(Function(x) CType(x, String))
            newSenseElement.SenseRestrictedToReading = xSenseElement.Elements("stagr").Select(Function(x) CType(x, String))
            newSenseElement.CrossReference = xSenseElement.Elements("xref").Select(Function(x) CType(x, String))
            newSenseElement.Antonym = xSenseElement.Elements("ant").Select(Function(x) CType(x, String))
            newSenseElement.Field = xSenseElement.Elements("field").Select(Function(x) CType(x, String))
            newSenseElement.Dialect = xSenseElement.Elements("dial").Select(Function(x) CType(x, String))
            newSenseElement.SenseInformation = xSenseElement.Elements("s_inf").Select(Function(x) CType(x, String))
            newSenseElement.Misc = xSenseElement.Elements("misc").Select(Function(x) CType(x, String))

            newSenseElement.LanguageSource = xSenseElement.Elements("lsource").Select(Function(x) LanguageSourceElement.Parse(x))
            Return newSenseElement
        End Function
    End Class

    Public Class GlossElement
        Public Property Language As String
        Public Property GlossType As String
        Public Property Text As String

        Public Shared Function Parse(xGlossElement As XElement) As GlossElement
            Dim newGlossElement As New GlossElement

            newGlossElement.Language = CType(xGlossElement.Attributes().Where(Function(x) x.Name.LocalName = "lang").FirstOrDefault(), String)
            newGlossElement.GlossType = CType(xGlossElement.Attribute("g_type"), String)
            newGlossElement.Text = CType(xGlossElement, String)
            Return newGlossElement
        End Function
    End Class

    Public Class LanguageSourceElement

        Public Property Language As String
        Public Property LanguageSourceType As String

        Public IsWaseieigo As Boolean

        Public Property Text As String

        Public Shared Function Parse(xLanguageSourceElement As XElement) As LanguageSourceElement
            Dim newLanguageSourceElement As New LanguageSourceElement
            newLanguageSourceElement.Language = CType(xLanguageSourceElement.Attributes().Where(Function(x) x.Name.LocalName = "lang").FirstOrDefault(), String)
            newLanguageSourceElement.LanguageSourceType = CType(xLanguageSourceElement.Attribute("ls_type"), String)
            newLanguageSourceElement.IsWaseieigo = IIf(CType(xLanguageSourceElement.Attribute("ls_wasei"), String) = "y", True, False)
            newLanguageSourceElement.Text = CType(xLanguageSourceElement, String)
            Return newLanguageSourceElement
        End Function

    End Class
End Module

推荐问答