Output XmlDocument as XML - vb.net

I have a function which reads a string to an XMLDocument.
I wish to then output the XML in that document to the screen.
Dim L As String = P.ToString()
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.LoadXml(L)
Context.Response.Write(xmlDoc)
the above does not work, I have also tried using
Return(xmlDoc)
with no success. How should I perform this function? Should I not be using XMLDocument?

One of the possible ways is to use a StringWriter:
Using stringWriter = New StringWriter()
Using xmlTextWriter = XmlWriter.Create(stringWriter)
xmlDoc.WriteTo(xmlTextWriter)
xmlTextWriter.Flush()
result = stringWriter.GetStringBuilder().ToString()
End Using
End Using
On the example above, your XML will be stored into the result variable.
The C# equivalent code may be found on this thread.

StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
document.WriteTo(xmlTextWriter);
Console.WriteLine(stringWriter.ToString());
Console.Read();

Related

HTTP Get in VB.Net how do I pass in the code from the body?

There are a lot of examples of how to add parameters into WebClient but how do you pass in the code you have in the body (see my response below with image of what it looks like in Postman)? Below is my code:
Using client As New WebClient()
client.Headers(HttpRequestHeader.Authorization) = AuthToken
client.QueryString.Add("parameter1", "Hello world") 'Pass in parameter, but not sure how to pass in code from Body
Dim json As String = client.DownloadString("https://www.zohoapis.com/crm/v2/Products")
Dim ds1 As DataTable = GetDataTableFromJsonString(json)
Dim BindingSource1 = New BindingSource
BindingSource1.DataSource = ds1
DataGridView1.DataSource = BindingSource1
End Using
Any help would be greatly appreciated.
Thanks,
Paul
I tried client.QueryString.Add() to see if I can just put that string in there but it didn't work.

How can I use a Resource as a string parameter?

I want to use an XML-File for my Dataset to save changes.
datatable.ReadXml("C:\test.xml")
works fine. But I want to use an Xml Resource file. So I tried
Dim xmlDoc as String
xmlDoc = MyApplication.My.Resources.xmlTestFile
datatable.ReadXml(xmlDoc)
gives me the error Illigal sign in path and debugging shows me that xmlDoc is empty.
Could anyone help me get this working?
ReadXml requires a filename not content
use
Dim ds As New DataSet()
Using stringReader As New StringReader(.......RESOURCE.HERE.......)
ds = New DataSet()
ds.ReadXml(stringReader)
End Using
Dim dt As DataTable = ds.Tables(0)

How do I read text directly from a URL using VB.net?

I'm making a Windows Phone 8 app. I have the latitude and longitude of the target location. I need to get the Two Letter ISO country code of target location.
I'm using the following code to make it happen.
'Dim address As String = puri
'Dim client As WebClient = New WebClient()
'Dim reader As StreamReader = New StreamReader(address)
'code = reader.ReadToEnd
Dim inStream As StreamReader
Dim wr As WebRequest
Dim webresponse As WebResponse
wr = WebRequest.Create(puri)
webresponse = wr.GetResponse()
inStream = New StreamReader(webresponse.GetResponseStream())
code = inStream.ReadToEnd()
where puri(in the commented code) is the address of the webservice in string format.
When trying the commented code, the error I'm getting is that string cannot be converted to system.uri format. (address)
When trying the uncommented code, I get an error which says, getresponse is not a member of class system.net.webrequest()
I guess with the updates to .NET the code changed, but I couldn't find anything current on the topic.
URI = http://api.geonames.org/countryCode?lat=17.60890&lng=76.98966&username=demo
I think you should use WebClient Class instead of a WebRequest. It is simpler and faster. Here is a simple example:
Dim WebCL As New WebClient
Dim DownLoadedText As String = String.Empty
Try
DownLoadedText = WebCL.DownloadString("Your Url")
' Do something
Catch ex As Exception
Throw New Exception("Oops!! ERROR has occured, something is wrong with your address")
End Try

How to read returned stream data

I am currently reading a stream like this:
wc = New System.Net.WebClient()
strm = wc.OpenRead(url)
Data.ReadXml(strm, XmlReadMode.InferSchema)
strm.Close()
Besides the Dataset reading the stream I would also like to be able to output the text to the log file. Or for testing to a debug.print statement.
How can I do that? I have tried adding a new stream reader after the wc.openread but then there is no data for the dataset to read.
Any help would be appreciated..
Rick
after the
Data.ReadXml(strm, XmlReadMode.InferSchema)
call the Data.GetXml
Dim xmlString As String = Data.GetXml
Sample below,
Dim Data As New DataSet
Using wc = New System.Net.WebClient()
Using strm = wc.OpenRead("http://news.yahoo.com/rss/entertainment")
Data.ReadXml(strm, XmlReadMode.InferSchema)
Dim xmlString As String = Data.GetXml
Debug.WriteLine(xmlString)
End Using
End Using

Parse Google GeoCode xml in VB.net

Ok so I got my little chunck of code that goes out and brings back xml from google for my location after i send it the lat and long. What I now need to be able to do is just get one line out of it.
From the second result set I need to get formatted_address.
Never done any xml parsing in vb.net so I am way lost.
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://maps.googleapis.com/maps/api/geocode/xml?latlng=44.46944050638752,-88.08779155546756&sensor=false")
Return result
After that I am lost!
In case anyone needs to know. I figured it out. Here is what i did:
Dim myAddress As String = "Nothing"
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://maps.googleapis.com/maps/api/geocode/xml?latlng=lat,long&sensor=false")
Dim xmlDoc As New XmlDocument()
xmlDoc.LoadXml(result)
Dim m_nodelist As XmlNodeList
m_nodelist = xmlDoc.SelectNodes("/GeocodeResponse/result/formatted_address")
myAddress = m_nodelist(1).InnerText
result = myAddress