기타 [VB.NET] XML Parsing(파싱) - XML 추출하기
페이지 정보

본문
아래외 같은 XML 자료가 있을경우
updatecheck의 Version을 파싱할려고 합니다.
<?xml version="1.0" encoding="UTF-8"?>
<gupdate xmlns="http://www.google.com/update2/response" protocol="2.0" server="prod">
    <daystart elapsed_days="5708" elapsed_seconds="55622" />
    <app appid="{8A69D345-D564-463C-AFF1-A69D9E530F96}" cohort="1:gu:178f@0.5" cohortname="Stable" status="ok">
        <updatecheck Version="104.0.5112.81" arguments="--verbose-logging --do-not-launch-chrome --channel=stable" codebase="https://redirector.gvt1.com/edgedl/release2/chrome/acryzwk4bzyqlfwvkoxjvflmon2q_104.0.5112.81/104.0.5112.81_chrome_installer.exe" fp="1.4ee180631b32a8b9ca2f0b48abf9c0aa3a92b5a13739e88f5e04df25719a5ec4" hash="0bbbkhjzDFaOFu1sV4QeorJp2LY=" hash_sha256="4ee180631b32a8b9ca2f0b48abf9c0aa3a92b5a13739e88f5e04df25719a5ec4" needsadmin="false" onsuccess="exitsilentlyonlaunchcmd" size="85634648" status="ok" />
    </app>
</gupdate>
아래와 같이 파싱하면 됩니다.
Dim RP As nhnRequestParameters = DirectCast(Deserialize(profileName, New nhnRequestParameters), nhnRequestParameters)
RP.Url = "https://update.googleapis.com/service/update2?"
RP.SetHeaders = "Host: update.googleapis.com"
RP.SetPostData = "<?xml version=""1.0"" encoding=""UTF-8""?><request><os platform=""win"" version=""10.0.19044.1889"" arch=""x64""/><app appid=""{8A69D345-D564-463C-AFF1-A69D9E530F96}"" lang=""ko"" ><updatecheck /></app></request>"
RP.nhnWinHttp()
Dim DocumentXml As New Xml.XmlDocument
DocumentXml.LoadXml(RP.ResponseText)
Dim DocumentElement As Xml.XmlElement = DocumentXml.DocumentElement
Dim updatecheck As Xml.XmlElement = DocumentElement.GetElementsByTagName("updatecheck")(0)
Dim Version As String = updatecheck.Attributes("Version").Value
Imports System.Runtime.CompilerServices
Imports WinHttp
Public Module _WinHttp_
    Private WithEvents nhnHttp As WinHttpRequest
    Private Sub WinHttp_OnError(ErrorNumber As Integer, ErrorDescription As String) Handles nhnHttp.OnError
        LOG(String.Format("[{0}] {1}", ErrorNumber, ErrorDescription)) : Sleep(1000)
    End Sub
    Private Sub WinHttp_OnResponseDataAvailable(ByRef Data As Array) Handles nhnHttp.OnResponseDataAvailable
    End Sub
    Private Sub WinHttp_OnResponseFinished() Handles nhnHttp.OnResponseFinished
    End Sub
    Private Sub WinHttp_OnResponseStart(Status As Integer, ContentType As String) Handles nhnHttp.OnResponseStart
    End Sub
    <Extension()> Public Function nhnWinHttp(ByRef rp As nhnRequestParameters) As Boolean
        Dim tem() As String
        If nhnHttp Is Nothing Then
            Try
                nhnHttp = New WinHttpRequest
            Catch ex As Exception
            End Try
        End If
        Do
            nhnWinHttp = False
            Try
                With nhnHttp
                    .SetTimeouts(3000, 3000, 3000, 3000)
                    .Open(rp.Method, rp.Url)
                    For Each hd As String In rp.Headers
                        tem = Split(hd, ": ")
                        .SetRequestHeader(tem(0).Trim, tem(1).Trim)
                    Next
                    If Len(rp.Cookie) Then .SetRequestHeader("Cookie", rp.Cookie)
                    .Send(rp.PostData)
                    If Not .WaitForResponse(0) Then Continue Do
                    rp.ResponseHeaders = .GetAllResponseHeaders
                    rp.Cookie = rp.Cookie.SetCookie(.GetAllResponseHeaders)
                    'T = System.Text.Encoding.Default.GetString(.ResponseBody)
                    rp.ResponseText = .ResponseText
                End With
                nhnWinHttp = True
            Catch ex As Exception
                Application.DoEvents()
            End Try
        Loop Until nhnWinHttp
    End Function
End Module
Public Class nhnRequestParameters
    Public Property _Url_ As String
    Public Property Headers() As List(Of String)
    Public Property PostData As String
    Public Property Cookie As String
    Public Property Method As String
    Public Property ResponseHeaders As String
    Public Property ResponseText As String
    Public Sub New()
        Me.Cookie = ""
        Me.PostData = ""
        Me.Method = "GET"
    End Sub
    Public Property Url As String
        Set(value As String)
            Me._Url_ = value
            Me.Method = "GET"
            Me.PostData = ""
        End Set
        Get
            Return Me._Url_
        End Get
    End Property
    Public WriteOnly Property SetPostData As String
        Set(value As String)
            Me.PostData = ""
            If value IsNot Nothing AndAlso value.Length > 0 Then Me.PostData = value
            Me.Method = "POST"
        End Set
    End Property
    Public WriteOnly Property SetHeaders As String
        Set(value As String)
            Me.Headers = value.Split("|").ToList
        End Set
    End Property
End Class
- 이전글[VB.NET] 윈도우 프로그램과 폼안의 객체만 따로 캡쳐하기 22.09.06
 - 다음글[WinHttp] WinHttpRequest 22.08.19
 
댓글목록
등록된 댓글이 없습니다.



