2011-04-15 47 views
0

新手尋求幫助獲取xml http響應在vb.net 這就是我想要做的,獲取這些屬性values(Red,Green,Yellow,Black)到vb上的4個不同的文本框。淨項目。任何幫助學習這將是非常有益的。 感謝幫助擴展xml和獲取屬性值到文本框vb.net

<system ver="1.0"> 
<colors> 
<type red="Red" green="Green" yellow="Yellow" Black="Black" /> 
</colors> 
</system> 

這裏是我到目前爲止,並嘗試不同的方法,但總是結束了擦除它。:(

Sub GetData() 


     Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("target url") 
     request.Credentials = New System.Net.NetworkCredential("user", "pass") 
     Dim response As System.Net.HttpWebResponse = request.GetResponse() 
     If response.StatusCode = System.Net.HttpStatusCode.OK Then 

      Dim stream As Stream = response.GetResponseStream() 
      ' Create a reader for the stream object 
      Dim reader As New StreamReader(stream) 
      ' Read from the stream object using the reader, put the contents in a string 
      Dim contents As String = reader.ReadToEnd() 
      ' Create a new, empty XML document 
      Dim HttpResult As New XmlDocument 
      Dim UserInfo As XmlNodeList 
      Dim Discription As XmlNode 
      HttpResult = New XmlDocument 
      HttpResult.LoadXml(contents) 


      'Create the XML Document 
      HttpResult = New XmlDocument() 
      Dim _XPath As String = 
      UserInfo = HttpResult.SelectNodes("") 



      'Get the Gender Attribute Value 
      Dim DetailsAttribute = Discription.Attributes.GetNamedItem("").Value 
    endif 

    End Sub 

回答

1

開始與這個...

Dim MyDoc as New System.Xml.XmlDocument 
MyDoc.Load("c:\path\to\your\xml\file") 

Dim MyNode as System.Xml.XmlNode = MyDoc.SelectSingleNode("//system/colors/type") 

Dim RedAttribute as String = MyNode.Attributes("red").Value 
Dim GreenAttribute as String = MyNode.Attributes("green").Value 
Dim YellowAttribute as String = MyNode.Attributes("yellow").Value 
Dim BlackAttribute as String = MyNode.Attributes("black").Value 

我認爲這應該讓你到你想去的地方

+0

非常感謝:)工作正常 我不得不添加myDoc。 loadxml會導致它在沒有它的情況下添加其他非法字符。 現在唯一的問題是,它說它不能將紅色,綠色..屬性轉換爲字符串。關於如何將其轉換爲字符串的任何想法? 再次感謝很多的幫助! – Tom 2011-04-15 18:22:08