2017-06-01 9 views
0

XmlTextReaderを使用してXMLファイルを読み取るときにエラーが発生します。まず、URLは正常ですが、2番目のURLは失敗します。他のURLを使用するのは正常です。このURLのみにエラーがあります。XmlTextReaderから整数型例外が読み取られる

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 
+0

エラーとは何ですか、どこにスローされますか? –

+0

'euckr = System.Text.Encoding.GetEncoding(" euc-kr ")'を割り当てますが、決して使用しないでください。 'http://www.hrd.go.kr/... 'URLから返されるXMLは実際には(Firefoxによると)EUC-KRでエンコードされているので、それを使用する必要があります。それとも、[この回答](https://stackoverflow.com/a/30049848/3744182)の 'DownloadStringAwareOfEncoding()'を使ってください。 – dbc

+0

また、私は '' accept-langquage''は['' Accept-Language''](https://stackoverflow.com/a/13766549/3744182)であるべきだと思います。 – dbc

答えて

0

ウェブページは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 
+0

次のkludgeで終了タグにスラッシュがありません。titleIconStr = titleIconStr.Replace( "" "" "" "/>") – jdweng

関連する問題