fetch xml file from web server

I set up a web server to store an xml file. My application will fetch that xml file. What’s the correct way to do this job?

My current code is

Dim XMLHTTP As New HTTPSocket Dim xmlFile As FolderItem If XMLHTTP.Get("http://www.myserver.com/myxmlexample.xml", xmlFile, 15) = True Then LoadAndDisplay(xmlFile) End If

Where function “LoadAndDisplay()”

  XmlList.DeleteAllRows
  
  Dim xml As New XmlDocument
  
  Try
    xml.LoadXml(xmlFile)
  Catch e As XmlException
    MsgBox("This does not appear to be an XML file.")
    Return
  End Try

This function is coming directly from the example.

But I got an error when it loads the xmlFile, said the encoding is unknown or similar.

I double check the xmlFile before it get loaded and found that folder item is empty/Nil. But the XMLHTTP.Get did return a true value. what I am missing here?

What’s the most common procedure to fetch an xml file from the server?

The first thing that pops out at me is this. The xmlFile folderitem needs to be initialized to point to where you want the download to go. Change the above line to something like this:

Dim xmlFile As FolderItem = GetTemporaryFolderItem()

This will initialize the xmlFile folderitem to point to a new file in the user’s temporary folder.

[quote=288195:@Andrew Lambert]The first thing that pops out at me is this. The xmlFile folderitem needs to be initialized to point to where you want the download to go. Change the above line to something like this:

Dim xmlFile As FolderItem = GetTemporaryFolderItem()

This will initialize the xmlFile folderitem to point to a new file in the user’s temporary folder.[/quote]

Thanks, but I still have the question is do I have to store this xml file at somewhere on the hard drive?

In your code, the temporary folder, does the file delete when the process is done?

You don’t have to, no. You can download into a string variable by using a different Get method (there are several):

Dim XMLHTTP As New HTTPSocket
Dim xmlData As String
xmlData = XMLHTTP.Get("http://www.myserver.com/myxmlexample.xml", 15)

Yes, but only if/when the program exits normally. If it crashes then the file will not be deleted.

[quote=288218:@Andrew Lambert]You don’t have to, no. You can download into a string variable by using a different Get method (there are several):

Dim XMLHTTP As New HTTPSocket
Dim xmlData As String
xmlData = XMLHTTP.Get("http://www.myserver.com/myxmlexample.xml", 15)

[/quote]

One more question, if I download into a string variable, how can I parse it, like using the method of xmlDocument.LoadXml and etc? If I can’t using those, is there a method to convert the string variable into xmlDocument?

Thanks a lot!

@BO CHEN: Here’s how I’ve been doing it. In this example, I’ve pulled the XML source via an HTTP call, and stored in xmlData as a String.

[code]xmlData = xmlData.DefineEncoding(Encodings.UTF8)

Dim xmlDoc As New XMLDocument

Try
xmlDoc.LoadXML(xmlData)
Catch e As XmlException

// Handle the exception.

End Try[/code]

[quote=288363:@Tim Dietrich]@BO CHEN: Here’s how I’ve been doing it. In this example, I’ve pulled the XML source via an HTTP call, and stored in xmlData as a String.
[/quote]

Thanks Tim that’s exactly what I need.