Reading large XML file using XMLReader in VB.net

后端 未结 1 1521
执笔经年
执笔经年 2020-12-22 10:05

I have a XML file size 35 GB. I tried to load this file using xmldocument and got out of memory exception. So, using xmlreader to parse the xml data to load it to database.

相关标签:
1条回答
  • 2020-12-22 10:23

    Try following code which uses combination of XmlReader and xml linq

    Imports System.Xml
    Imports System.Xml.Linq
    Module Module1
        Const FILENAME As String = "c:\temp\test.xml"
        Sub Main()
            Dim wcProdcutions As New List(Of WCProduction)
            Dim reader As XmlReader = XmlReader.Create(FILENAME)
            While (Not reader.EOF)
                If reader.Name <> "wcproduction" Then
                    reader.ReadToFollowing("wcproduction")
                End If
                If Not reader.EOF Then
                    Dim xWcproduction As XElement = XElement.ReadFrom(reader)
                    Dim ns As XNamespace = xWcproduction.GetDefaultNamespace()
                    Dim wcproduction As New WCProduction
                    wcProdcutions.Add(wcproduction)
    
                    wcproduction.api_st_cde = CType(xWcproduction.Element(ns + "api_st_cde"), Integer)
                    wcproduction.api_well_idn = CType(xWcproduction.Element(ns + "api_well_idn"), Integer)
                    wcproduction.pool_idn = CType(xWcproduction.Element(ns + "pool_idn"), Integer)
                    wcproduction.prodn_mth = CType(xWcproduction.Element(ns + "prodn_mth"), Integer)
                    wcproduction.prodn_yr = CType(xWcproduction.Element(ns + "prodn_yr"), Integer)
                    wcproduction.ogrid_cde = CType(xWcproduction.Element(ns + "ogrid_cde"), Integer)
                    wcproduction.prd_knd_cde = CType(xWcproduction.Element(ns + "prd_knd_cde"), String)
                    wcproduction.eff_dte = CType(xWcproduction.Element(ns + "eff_dte"), DateTime)
                    wcproduction.amend_ind = CType(xWcproduction.Element(ns + "amend_ind"), String)
                    wcproduction.c115_wc_stat_cde = CType(xWcproduction.Element(ns + "c115_wc_stat_cde"), String)
                    wcproduction.prod_amt = CType(xWcproduction.Element(ns + "prod_amt"), Integer)
                    wcproduction.prodn_day_num = CType(xWcproduction.Element(ns + "prodn_day_num"), Integer)
                    wcproduction.mod_dte = CType(xWcproduction.Element(ns + "mod_dte"), DateTime)
    
                End If
    
            End While
        End Sub
    
    End Module
    Public Class WCProduction
        Public api_st_cde As Integer
        Public api_cnty_cde As Integer
        Public api_well_idn As Integer
        Public pool_idn As Integer
        Public prodn_mth As Integer
        Public prodn_yr As Integer
        Public ogrid_cde As Integer
        Public prd_knd_cde As String
        Public eff_dte As DateTime
        Public amend_ind As String
        Public c115_wc_stat_cde As String
        Public prod_amt As Integer
        Public prodn_day_num As Integer
        Public mod_dte As DateTime
    End Class
    
    0 讨论(0)
提交回复
热议问题