从VB.net中的XML中提取信息

时间:2016-05-11 23:47:12

标签: xml vb.net

我已经使用了这个运行的代码:

module Foo

(*Types*)
type Position =
    | First
    | Second
    | Third 

type Player =
    | Scott
    | Brian
    | Cherice

type Hit =
    | Single
    | Double
    | Triple

type Play = { Player: Player; Hit: Hit }

type Bases = { 
    First:Player  option
    Second:Player option
    Third:Player  option
}

(*Functions*)
let assignBases (plays:Play list) =

    let initializedBase = { First=None; Second=None; Third=None }

    let move bases player =

        match bases with
        | { First= None;   Second=None;   Third=None }   -> { bases with First=player }
        | { First= player; Second=None;   Third=None }   -> { bases with First=player; Second=bases.First }
        | { First= None;   Second=player; Third=None }   -> { bases with First=None; Second=None; Third=player }
        | { First= None;   Second=None;   Third=player } -> { bases with First=player }
        | _ -> initializedBase // fill the rest out later...

    (initializedBase, plays) ||> List.fold (fun bases play -> 
                                                Some play.Player |> move bases)

(*Tests*)
open NUnit.Framework
open FsUnit

[<Test>]
let ``2 players each hit single``() =
    // Setup
    let players = [{ Player= Scott; Hit= Single }; { Player= Brian; Hit= Single }]

    // Test
    let bases = players |> assignBases

    bases |> should equal { First=Some Brian; Second=Some Scott; Third=None }

返回此xml:

Using client As New Net.WebClient
    Dim reqparm As New Specialized.NameValueCollection
    reqparm.Add("username", "username")
    reqparm.Add("password", "password")
    Dim responsebytes = client.UploadValues("www.example.com", "POST", reqparm)
    Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
End Using

我希望获取会话ID并将其存储在变量中。我如何从XML中提取它?

2 个答案:

答案 0 :(得分:0)

在.NET中有很多方法可以读取XML,这里有一个选项。

Private Function GetSessionId(text As String) As String
    Using StringReader As New StringReader(text)
        Using XmlReader As XmlReader = XmlReader.Create(StringReader)
            If XmlReader.ReadToFollowing("sessionid") Then
                Return XmlReader.ReadElementContentAsString()
            End If
        End Using
    End Using

    Return String.Empty
End Function

用法看起来像这样..

Dim SessionId As String = GetSessionId(responsebody)

答案 1 :(得分:0)

有几种方法,可以使用XElement对象完成一个非常简单的方法:

VB.NET

Dim xmlTree As XElement = <params>
<status>...</status>
<error>...</error>
<version>...</version>
<sessionid>...</sessionid>
<sessiontimeout>...</sessiontimeout>
<staffid>...</staffid>
<uniqueid>...</uniqueid>
<option name="...">...</option>
<permission name="...">...</permission>
<variable name="...">...</variable>
</params>

然后你可以像这样访问字段:

Dim result = xmlTree.<status>.Value