2017-05-16 5 views
0

Im ein Webformular in vb.net zu tun, ich bin ein webservice raubend, was mich zurück zu allen LändernKonsumieren von Daten von Webservice mit vb.net

haben nur 1 Knopf Enviar, die die Länder aufrufen.

Imports service_country = WebServiceVB2.country 

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     Dim serv_country As New service_country.country '--Create object' 
     Dim MyDoc As New System.Xml.XmlDocument 
     Dim MyXml As String = serv_country.GetCountries() '--Execute procedure from webservice' 

     MyDoc.LoadXml(MyXml) '--Read Myxml and convert to XML' 
     Dim SymbolText As String = MyDoc.SelectSingleNode("//NewDataSet/Table/Name").InnerText '--select the node' 
     Label1.Text = SymbolText 

    End Sub 

Meine Frage ist Wie kann ich alle Werte auswählen, die in der ‚Name‘ sind. Eigentlich zeigt es nur eins. Beispiel:

introducir la descripción de la imagen aquí

Vielen Dank im Voraus.

+0

So etwas wie 'Dim oNode1 Wie Xml.XmlNodeList = MyDoc.SelectNodes ("// NewDataSet/Table/Name") ' – Hackerman

Antwort

1

Dies war ein interessantes Problem. Da Daten als Webseite kommen, kam die offene Klammer als "& l t;" während die schließende Klammer kam als "& g t;". Also mussten diese ersetzt werden. Ich verwendete xml Linq, um die Namen zu erhalten:

Imports System.Xml 
Imports System.Xml.Linq 

Module Module1 
    Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries" 
    Sub Main() 
     Dim doc1 As XDocument = XDocument.Load(URL) 

     Dim docStr As String = doc1.ToString() 
     docStr = docStr.Replace(">", ">") 
     docStr = docStr.Replace("&lt;", "<") 

     Dim doc2 As XDocument = XDocument.Parse(docStr) 

     Dim root As XElement = doc2.Root 
     Dim defaultNs As XNamespace = root.GetDefaultNamespace() 
     Dim names() As String = doc2.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray() 
    End Sub 
End Module 

Mit WebUtility

Imports System.Xml 
Imports System.Xml.Linq 
Imports System.Text 
Imports System.Net 


Module Module1 
    Const URL As String = "http://www.webservicex.net/country.asmx/GetCountries" 
    Sub Main() 

     Dim xReader As XmlReader = XmlTextReader.Create(URL) 
     xReader.MoveToContent() 
     Dim doc As XDocument = XDocument.Parse(WebUtility.HtmlDecode("<?xml version=""1.0"" encoding=""iso-8859-9"" ?>" & xReader.ReadOuterXml)) 

     Dim root As XElement = doc.Root 
     Dim defaultNs As XNamespace = root.GetDefaultNamespace() 
     Dim names() As String = doc.Descendants(defaultNs + "Name").Select(Function(x) CType(x, String)).ToArray() 
    End Sub 
End Module 
+0

Danke, ich habe gerade' For Each' hinzugefügt, um den Inhalt zu lesen. Es war sehr hilfreich. – cport93

Verwandte Themen