Deserialize an XML stream

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
The Service: http://www.service-repository.com/service/overview/-2082028434

Needing some real help here guys. I'm pulling a stream from a web service and this is what I'm getting:

- <NewDataSet>
- <Table>
<Country>South Africa</Country>
<City>Alexander Bay</City>
</Table>
- <Table>
<Country>South Africa</Country>
<City>Waterkloof Lmb</City>
</Table>
- <Table>
<Country>South Africa</Country>
<City>Welkom</City>
</Table>
</NewDataSet>

I want to dump the city names into a drop down list. This is what I have so far:


Code:
Public Class Product
    Public Property Country() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = value
        End Set
    End Property
    Private m_Id As Integer
    Public Property City() As String
        Get
            Return m_Name
        End Get
        Set(value As String)
            m_Name = value
        End Set
    End Property
    Private m_Name As String
End Class


Code:
Dim proxySample As New com.webservicex.www.GlobalWeather  ' Proxy object.
        Dim webreplystring As String = proxySample.GetCitiesByCountry("South Africa")


        Dim serializer As New XmlSerializer(GetType(List(Of Product)), New XmlRootAttribute("Table"))
        Dim stringReader As New StringReader(webreplystring)
        Dim productList As List(Of Product) = DirectCast(serializer.Deserialize(stringReader), List(Of Product))

This is the error I'm getting and I can't figure this out:

{"<NewDataSet xmlns=''> was not expected."}
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
Ignore all of that, problem solved.

Code:
        Dim xelement As XElement = xelement.Parse(webreplystring)

        Dim products As IEnumerable(Of XElement) = xelement.Descendants("City")

        For Each product As XElement In products
            ListBox1.Items.Add(product.Value)
        Next
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
I would have gone with an XML Deserialization solution, something similar to this: http://stackoverflow.com/questions/364253/how-to-deserialize-xml-document

Oh Snap! That one is on one of my open tabs. Having a look now :)

I've managed to get it 50% working. The Town names are pulling from the web service, however the Country names are hard coded (cringe). Overall it needs to be dynamic.

That code solution you linked I'm going to go with to make it more efficient overall. I will post as I go along.

000123.jpg
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
Thanks Bighit. This is what I came up with and it works

C#
Code:
private void GetCountryList()
{
	try {

		CountryService.country proxySample = new CountryService.country();

		XElement xelement = xelement.Parse(CountryService.country());

		Dim products As IEnumerable(Of XElement) = xelement.Descendants("Name")

		ComboBox1.Items.Clear();

		foreach (XElement product in products) {
			ComboBox1.Items.Add(product.Value);
		}

	} catch (Exception ex) {
		MessageBox.Show("Unable to connect to the web server.  Please try again later.");
	} 

}


VB
Code:
    Private Sub GetCountryList()
        Try

            Dim proxySample As New CountryService.country  ' Proxy object.
            Dim xelement As XElement = xelement.Parse(proxySample.GetCountries())
            Dim products As IEnumerable(Of XElement) = xelement.Descendants("Name")

            ComboBox1.Items.Clear()

            For Each product As XElement In products
                ComboBox1.Items.Add(product.Value)
            Next

        Catch ex As Exception
            MessageBox.Show("Unable to connect to the web server.  Please try again later.")
        End Try
 
Last edited:

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
42,928
Side note: why are you populating a UI control in a function called Get?

/pedantic
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,887
Side note: why are you populating a UI control in a function called Get?

/pedantic

VB
Code:
Public Function CountryList() As List(Of [String])
        Try

            Dim namesList As New List(Of [String])()
            Dim proxySample As New CountryService.country  ' Proxy object.
            Dim webreplystring As String = proxySample.GetCountries()
            Dim xelement As XElement = xelement.Parse(webreplystring)
            Dim products As IEnumerable(Of XElement) = xelement.Descendants("Name")

            For Each product As XElement In products
                namesList.Add(product.Value)
            Next

            Return namesList

            proxySample.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message())
        End Try

    End Function

Code:
        Dim keys As New List(Of String)(CountryList())
        For Each key As String In keys
            cboCountry.Items.Add(key.ToString())
        Next



C#
Code:
public List<String> CountryList()
{

	try {
		List<String> namesList = new List<String>();
		CountryService.country proxySample = new CountryService.country();
		// Proxy object.
		string webreplystring = proxySample.GetCountries();
		XElement xelement = xelement.Parse(webreplystring);
		IEnumerable<XElement> products = xelement.Descendants("Name");

		foreach (XElement product in products) {
			namesList.Add(product.Value);
		}

		return namesList;

		proxySample.Dispose();

	} catch (Exception ex) {
		MessageBox.Show(ex.Message());
	}

}

Code:
List<string> keys = new List<string>(CountryList());
foreach (string key in keys) {
cboCountry.Items.Add(key.ToString());
}
 
Last edited:
Top