【问题标题】:Integer Exception from XmlTextReader read来自 XmlTextReader 的整数异常读取
【发布时间】:2017-06-01 05:10:18
【问题描述】:

使用 XmlTextReader 读取 XML 文件时出现错误。第一个网址正常,第二个网址失败。使用其他 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

【问题讨论】:

  • 错误是什么,在哪里抛出?
  • 您分配了一个euckr = System.Text.Encoding.GetEncoding("euc-kr"),但从不使用它。 http://www.hrd.go.kr/... URL 返回的 XML 确实是使用 EUC-KR 编码的(根据 Firefox),因此您需要使用它。或者更好的是使用来自this answerDownloadStringAwareOfEncoding()
  • 另外,我认为"accept-langquage" 应该是"Accept-Language"

标签: asp.net xml vb.net xmltextreader


【解决方案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

【讨论】:

  • 我修复了结束标签中缺少的正斜杠,并带有以下内容:titleIconStr = titleIconStr.Replace(""">", """/>")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
相关资源
最近更新 更多