WebService returns XML in a String - how can I 'convert' this to an XML document?

B

Ben Turner

I'm querying a web service to get some global weather data (http://www.webservicex.net/globalweather.asmx?WSDL) which works really well, however I'm getting stuck on the returned data for the GetWeather call.

The data returned is presented as a string but actually contains XML data. I'd like to treat the data as XML, and then save it to a file, append other results etc. so I can re-use the data later without having to requery the server (to save time etc.)

But I'm really new to XML and don't know where to start. I've managed to get the data into an XML with the code shown at the bottom, but I need to manipulate the XML (add some nodes, change some, delete some etc.) and I'd really like to do this before I save the file, rather than save it, then re-load it.

Is there a simple way I can just create an XML object (XMLDocument?) and just drop the contents of the string into it? Thanks.

sWeatherXML = wsNewWeather.GetWeather("Las Vegas", "United States")
Dim fs As New FileStream("Weather.xml", FileMode.OpenOrCreate)
Dim xw As New XmlTextWriter(fs, Nothing)
xw.WriteStartDocument()
xw.WriteRaw(sWeatherXML)
xw.Close()

Ben Turner
(e-mail address removed)
 
K

Ken Cox [Microsoft MVP]

Hi Ben,

An approach that I prefer in this case is to treat the everything as a
dataset and datatables. I find it easier to manipulate than XML nodes.

For example, your weather XML is quite simple in format. You can put XML
from the ws into a dataset and then add another table with your own data.
After that, just write it to the file.

The advantage is that you can read it back into a page and manipulate it as
data rather than as XML.

Here's the key part:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim wsNewWeather As New net.webservicex.www.GlobalWeather
Dim sreader As New System.IO.StringReader _
(wsNewWeather.GetWeather("Las Vegas", "United States"))
Dim ds As New DataSet
Dim dt As New DataTable
Dim dr As DataRow
' Read the XML from the ws into the dataset
ds.ReadXml(sreader)
' Give the new table a name
dt.TableName = "BenData"
' Add a couple of columns to the new table
dt.Columns.Add(New DataColumn _
("BenInteger", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("BenString", GetType(String)))
' Add a row to the table
dr = dt.NewRow
' Fill the row with data
dr.Item("BenInteger") = 10
dr.Item("BenString") = "Hello"
' Add the row to the table
dt.Rows.Add(dr)
' Add the table to the dataset
ds.Tables.Add(dt)
' Write the whole dataset to the file
ds.WriteXml("c:\dsetwser.xml")
End Sub

This produces:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<CurrentWeather>
<Location>Las Vegas, Las Vegas Municipal Airport, NM, United States
(KLVS) 35-39-15N 105-08-33W 2091M</Location>
<Time>Jan 01, 2004 - 11:53 PM EST / 2004.01.02 0453 UTC</Time>
<Wind> from the SSE (160 degrees) at 29 MPH (25 KT):0</Wind>
<Visibility> 10 mile(s):0</Visibility>
<SkyConditions> clear</SkyConditions>
<Temperature> 39.0 F (3.9 C)</Temperature>
<DewPoint> 21.9 F (-5.6 C)</DewPoint>
<RelativeHumidity> 49%</RelativeHumidity>
<Pressure> 29.98 in. Hg (1015 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
<BenData>
<BenInteger>10</BenInteger>
<BenString>Hello</BenString>
</BenData>
</NewDataSet>

Does this help?

Ken


I'm querying a web service to get some global weather data
(http://www.webservicex.net/globalweather.asmx?WSDL) which works really
well, however I'm getting stuck on the returned data for the GetWeather
call.

The data returned is presented as a string but actually contains XML data.
I'd like to treat the data as XML, and then save it to a file, append other
results etc. so I can re-use the data later without having to requery the
server (to save time etc.)

But I'm really new to XML and don't know where to start. I've managed to
get the data into an XML with the code shown at the bottom, but I need to
manipulate the XML (add some nodes, change some, delete some etc.) and I'd
really like to do this before I save the file, rather than save it, then
re-load it.

Is there a simple way I can just create an XML object (XMLDocument?) and
just drop the contents of the string into it? Thanks.

sWeatherXML = wsNewWeather.GetWeather("Las Vegas", "United States")
Dim fs As New FileStream("Weather.xml", FileMode.OpenOrCreate)
Dim xw As New XmlTextWriter(fs, Nothing)
xw.WriteStartDocument()
xw.WriteRaw(sWeatherXML)
xw.Close()

Ben Turner
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top