使用VB.net从Web页面重写XML

时间:2014-06-13 15:16:36

标签: xml vb.net

我正在尝试阅读网站生成的XML代码,但它总是失败,我不是一个主要在Server Domain工作的程序员。能帮我解决这个问题。

我要做的是,我正在制作一个小工具,它可以帮助我从Adobe Connect创建报告。我指的是Adobe Connect 9 Web服务API。 (http://help.adobe.com/en_US/connect/9.0/webservices/connect_9_webservices.pdf

早些时候我曾经得到407认证错误,但是现在使用下面的代码,我认为它使用默认信令进行身份验证。

Imports System.Net
Imports System.Xml


Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim theProxy As IWebProxy = WebRequest.DefaultWebProxy
        Dim aResp As HttpWebResponse = TryCast(WebRequest.GetSystemWebProxy, HttpWebResponse)
        ' Print the Proxy Url to the console.

        If theProxy IsNot Nothing Then
            theProxy.Credentials = CredentialCache.DefaultCredentials
        End If

        Try
            Dim reader As XmlTextReader = New XmlTextReader("https://my.adobeconnect.com/api/xml?action=common-info")
            'MsgBox("Authentnticated")

            Do While (reader.Read())
                Select Case reader.NodeType
                    Case XmlNodeType.Element 'Display beginning of element.
                        MsgBox("<" + reader.Name)
                        If reader.HasAttributes Then 'If attributes exist
                            While reader.MoveToNextAttribute()
                                'Display attribute name and value.
                                MsgBox(" {0}='{1}'", reader.Name, reader.Value)
                            End While
                        End If
                        MsgBox(">")
                    Case XmlNodeType.Text 'Display the text in each element.
                        MsgBox(reader.Value)
                    Case XmlNodeType.EndElement 'Display end of element.
                        MsgBox("</" + reader.Name)
                        MsgBox(">")
                End Select
            Loop
            Console.ReadLine()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

以上网址在网页上生成以下XML

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <status code="ok"/>
    <OWASP_CSRF_TOKEN><token/></OWASP_CSRF_TOKEN>
    <common time-zone-java-id="UTC" time-zone-id="85" locale="en">
        <cookie>XXXXXXXXXXXXXX</cookie>
        <date>2014-06-13T15:07:03.573+00:00</date>
        <host>https://my.adobeconnect.com</host>
        <local-host>pcparapp04</local-host>
        <admin-host>arcps.adobeconnect.com</admin-host>
        <url>/api/xml?action=common-info</url>
        <version>9.2.2</version>
        <product-notification>true</product-notification>
        <account account-id="295153"/>
        <user-agent>Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0; EIE10;ENINWOL)</user-agent>
        <mobile-app-package>air.com.adobe.connectpro</mobile-app-package>
    </common>
    <reg-user>
        <is-reg-user>false</is-reg-user>
    </reg-user>
</results>

我需要从上面的XML中获取标记的内容信息。

嘿克里斯,所有的代码都像魅力一样。谢谢你的帮助。

只是一个小问题,如何在名称中使用特殊字符检查标记的属性。 例如:

所以我写的时候

dim x = z .. @ sco-id

它不接受sco-id,我如何使用@ sco-id或@ transaction-id等属性名称

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果你正在使用最新版本的VB(2008或更高版本),你可以使用一种叫做XML文字的东西,这会让生活变得非常简单:

    Dim RemoteURL = "https://my.adobeconnect.com/api/xml?action=common-info"
    Dim X = XDocument.Load(RemoteURL)
    Dim Cookie = X.<results>.<common>.<cookie>.Value

如果您需要使用代理代码,您可以使用下载字符串并解析它的较长版本:

    Dim remoteURL = "https://my.adobeconnect.com/api/xml?action=common-info"
    Dim remoteText As String = Nothing
    Using WC As New System.Net.WebClient()
        Dim theProxy As IWebProxy = WebRequest.DefaultWebProxy
        theProxy.Credentials = CredentialCache.DefaultCredentials
        WC.Proxy = theProxy
        remoteText = WC.DownloadString(remoteURL)
    End Using

    If String.IsNullOrWhiteSpace(remoteText) Then
        Throw New ApplicationException("No data was returned from remote URL")
    End If

    Dim X = XDocument.Parse(remoteText)
    Dim Cookie = X.<results>.<common>.<cookie>.Value