Целочисленное исключение из чтения XmlTextReader

Я получаю сообщение об ошибке при чтении файла XML с помощью XmlTextReader. Первый URL-адрес нормальный, но второй URL-адрес не работает. Это нормально использовать другой URL. Только этот URL содержит ошибку.

    Dim url As String

    'url = "http://test.damiedu.net/testxml.xml"  
    'url = "http://www.hrd.go.kr/hrdp/api/apiao/APIAO0101T.do?authKey=k8V0j828FdFfjZye2mNR6tJ6kHIfkoJI&outType=1&srchTraEndDt=20150631&srchTraStDt=20150101&sortCol=TR_NM_i&returnType=XML&pageSize=20&pageNum=1&sort=ASC&srchTraPattern=C1&srchPart=-99&apiRequstPageUrlAdres=/jsp/HRDP/HRDPO00/HRDPOA40/HRDPOA40_1.jsp&apiRequstIp=211.179.124.14"

    Try

        Dim euckr As System.Text.Encoding

        euckr = System.Text.Encoding.GetEncoding("euc-kr")

        Dim Client As WebClient
        Dim test As Integer

        Client = New WebClient

        Client.Headers("accept-langquage") = "ko"

        Dim reader As XmlTextReader
        reader = New XmlTextReader(Client.OpenRead(url))

        test = reader.Read.ToString

        Do While reader.Read
            If (reader.NodeType = XmlNodeType.Element) Then

                If reader.Name = "address" Then
                    Response.Write("address" & reader.ReadElementString())

                End If

                If reader.Name = "instCd" Then
                    Response.Write("name" & reader.ReadElementString())

                End If


            End If

        Loop


    Catch ex As Exception
        MsgBox(ex.Message, "faile")
    End Try

person ej_lee    schedule 01.06.2017    source источник
comment
Что за ошибка и где она вылетает?   -  person Romano Zumbé    schedule 01.06.2017
comment
Вы выделяете euckr = System.Text.Encoding.GetEncoding("euc-kr"), но никогда не используете его. XML, возвращаемый URL-адресом http://www.hrd.go.kr/..., действительно закодирован с помощью EUC-KR (согласно Firefox), поэтому вам необходимо его использовать. Или еще лучше используйте DownloadStringAwareOfEncoding() из этого ответа.   -  person dbc    schedule 01.06.2017
comment
Кроме того, я считаю, что "accept-langquage" должно быть "Accept-Language".   -  person dbc    schedule 01.06.2017


Ответы (1)


Веб-страница gzip. Вот код ниже для разбора xml. Это не удается из-за того, что тег не закрыт. Не нашел способ обойти ошибку. Напишу, если найду решение

Imports System.Xml
Imports System.Xml.Linq
Imports System.Net
Imports System.IO
Module Module1

    Const URL As String = "http://www.hrd.go.kr/hrdp/api/apiao/APIAO0101T.do?authKey=k8V0j828FdFfjZye2mNR6tJ6kHIfkoJI&outType=1&srchTraEndDt=20150631&srchTraStDt=20150101&sortCol=TR_NM_i&returnType=XML&pageSize=20&pageNum=1&sort=ASC&srchTraPattern=C1&srchPart=-99&apiRequstPageUrlAdres=/jsp/HRDP/HRDPO00/HRDPOA40/HRDPOA40_1.jsp&apiRequstIp=211.179.124.14"

    Sub Main()

        Dim Client As New GZipWebClient(URL)

    End Sub

End Module

Public Class SCN_List
    Public Shared lists As New List(Of SCN_List)

    Public address As String
    Public courseMan As Integer
    Public grade As String
    Public imgGubun As String
    Public instCd As Long
    Public ncsCd As String
    Public realMan As Integer
    Public regCourseMan As Integer
    Public subTitle As String
    Public subTitleLink As String
    Public superViser As String
    Public telNo As String
    Public title As String
    Public titleIconImg As String
    Public titleIconAlt As String
    Public titleIcon As String
    Public titleLink As String
    Public traEndDate As DateTime
    Public traStartDate As DateTime
    Public trainTarget As String
    Public trprDegr As Integer
    Public trprId As String
    Public yardMan As Integer

End Class
Public Class GZipWebClient
    Inherits WebClient

    Sub New(URL As String)
        Dim reader As XmlTextReader = New XmlTextReader(OpenRead(URL))
        Dim newScnList As SCN_List
        While (Not reader.EOF)
            If reader.Name <> "scn_list" Then
                reader.ReadToFollowing("scn_list")
            End If
            If Not reader.EOF Then
                Dim xScn_List As XElement = XElement.ReadFrom(reader)

                newScnList = New SCN_List
                SCN_List.lists.Add(newScnList)

                newScnList.address = xScn_List.Element("address")
                newScnList.courseMan = xScn_List.Element("courseMan")
                newScnList.grade = xScn_List.Element("grade")
                newScnList.imgGubun = xScn_List.Element("imgGubun")
                newScnList.instCd = xScn_List.Element("instCd")
                newScnList.ncsCd = xScn_List.Element("ncsCd")
                newScnList.realMan = xScn_List.Element("realMan")
                newScnList.regCourseMan = xScn_List.Element("regCourseMan")

                newScnList.subTitle = xScn_List.Element("subTitle")
                newScnList.subTitleLink = xScn_List.Element("subTitleLink")
                newScnList.superViser = xScn_List.Element("superViser")
                newScnList.telNo = xScn_List.Element("telNo")
                newScnList.title = xScn_List.Element("title")
                Dim titleIconStr As String = xScn_List.Element("titleIcon")
                titleIconStr = titleIconStr.Replace(""">", """/>")
                Dim titleIconElement As XElement = XElement.Parse(titleIconStr)
                newScnList.titleIconImg = titleIconElement.Attribute("src")
                newScnList.titleIconAlt = titleIconElement.Attribute("alt")
                newScnList.titleIcon = titleIconElement.Attribute("title")

                newScnList.titleLink = xScn_List.Element("titleLink")
                newScnList.traEndDate = xScn_List.Element("traEndDate")
                newScnList.traStartDate = xScn_List.Element("traStartDate")
                newScnList.trainTarget = xScn_List.Element("trainTarget")
                newScnList.trprDegr = xScn_List.Element("trprDegr")
                newScnList.trprId = xScn_List.Element("trprId")
                newScnList.yardMan = xScn_List.Element("yardMan")

            End If
        End While

    End Sub

    Protected Overrides Function GetWebRequest(address As Uri) As WebRequest

        Dim request As HttpWebRequest = MyBase.GetWebRequest(address)
        request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
        Return request
    End Function

End Class
person jdweng    schedule 01.06.2017
comment
Я исправил отсутствующую косую черту в закрывающем теге с помощью следующего кладжа: titleIconStr = titleIconStr.Replace(›, /›) - person jdweng; 01.06.2017