使用XmlTextReader
读取XML文件时出错。首先,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
答案 0 :(得分: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