2011-04-15 65 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