When I create a normal application or class library I can use XDocument.Load(some xml file on the internet) to download and then parse the XML within that file.
However, I'm writing code for a PCL and it seems XDocument does not have the option of a string URI. The only options are System.IO.Stream, System.IO.TextReader, and System.Xml.XmlReader.
How should I proceed to be able to access an XML file on the internet? Should I make use of the new-ish HttpClient and use a stream from that somehow?
I think you need this code:
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://your_site.com/etc");
using (WebResponse response = http.GetResponse())
{
Stream stream = response.GetResponseStream();
XDocument xDoc = XDocument.Load(stream);
// Use the xDoc...
}
Related
I would like to load an excel file directly from an ftp site into a memory stream. Then I want to open the file in the FarPoint Spread control using the OpenExcel(Stream) method. My issue is I'm not sure if it's possible to download a file directly into memory. Anyone know if this is possible?
Yes, you can download a file from FTP to memory.
I think you can even pass the Stream from the FTP server to be processed by FarPoint.
WebRequest request = FtpWebRequest.Create("ftp://asd.com/file");
using (WebResponse response = request.GetResponse())
{
Stream responseStream = response.GetResponseStream();
OpenExcel(responseStream);
}
Using WebClient you can do nearly the same. Generally using WebClient is easier but gives you less configuration options and control (eg.: No timeout setting).
WebClient wc = new WebClient();
using (MemoryStream stream = new MemoryStream(wc.DownloadData("ftp://asd.com/file")))
{
OpenExcel(stream);
}
Take a look at WebClient.DownloadData. You should be able to download the file directory to memory and not write it to a file first.
This is untested, but something like:
var spreadSheetStream
= new MemoryStream(new WebClient().DownloadData(yourFilePath));
I'm not familiar with FarPoint though, to say whether or not the stream can be used directly with the OpenExcel method. Online examples show the method being used with a FileStream, but I'd assume any kind of Stream would be accepted.
Download file from URL to memory.
My answer does not exactly show, how to download a file for use in Excel, but shows how to create a generic-purpose in-memory byte array.
private static byte[] DownloadFile(string url)
{
byte[] result = null;
using (WebClient webClient = new WebClient())
{
result = webClient.DownloadData(url);
}
return result;
}
Dumb question, but anyway... I am wondering how can one load xml file from url (in Universal Project).
It was quite easy with WPF:
XmlDocument xml = new XmlDocument();
xml.Load(url);
but this isn't working for me here and I really can not find a way around, it's annoying.
Thanks in advance!
You can use HttpClient to do the request. (It's the Microsoft.Net.Http Nuget package.) Once you have a stream from it, there's an overload on XmlDocument.Load which accepts a stream. If you need it parsed as an object, skip XmlDocument.Load and use XmlSerializer instead.
using (HttpClientHandler hHandler = new HttpClientHandler())
{
HttpResponseMessage response = await hClient.GetAsync(URL);
System.IO.Stream oStrm = await response.Content.ReadAsStreamAsync();
XmlSerializer oSer = new XmlSerializer(typeof(T));
return (T)oSer.Deserialize(oStrm);
}
I have a javascript file in a remote server, and when I use httpwebrequest it returns some weird characters.
Thr url is http://goo.gl/0Ug5QI
Is this kind of compressed contents?
static string GetScriptSource(string _url)
{
string _retValue = string.Empty;
HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(_url);
hwr.Method = "GET";
HttpWebResponse res = (HttpWebResponse)hwr.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
return sr.ReadToEnd();
}
My code to read that script file's content is very simple.
Looking at the js source that you linked to, it could be that is has been gzipped. Try saving the source as a file and use 7zip or something to see if you can unzip it. There is a GZip library in C# so if it has been gzipped then you should be able to unzip it easily enough.
Although it's a Korean web site so maybe the encoding is not correct.
Either way it's not a problem with the code that you posted.
I am trying to pass through some XML from an external website.
What is the best way of doing this, through c# webpage or asp.MVC?
I tend to use something like this for working with external XML documents / RSS feeds etc:
string sURL = ".....";
// Create a request for the URL.
WebRequest oRequest = WebRequest.Create(sUrl);
// Get the response.
WebResponse oResponse = oRequest.GetResponse();
// Get the stream containing content returned by the server.
Stream oDataStream = oResponse.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader oReader = new StreamReader(oDataStream, System.Text.Encoding.Default);
// Read the content.
string sXML = oReader.ReadToEnd();
// Convert string to XML
XDocument oFeed = XDocument.Parse(sXML);
Either should be fine. MVC is probably easiest (in terms of getting a raw response), but you could do the same in regular ASP.NET just by using a handler (possibly .ashx), or just by clearing the response.
I am building a C# client for a CGI service (not sure what its called exactly).
It accepts a bunch of XML and spits out a response. I have tested it straight in Firefox and it works (see below).
Now I am not sure how to do this in C# code though? Does anyone have a helpful snippet of code, I can't imagine it would be that difficult?
http://www.travelcommunications.co.uk/cgi-bin/d3web_gzip.ssh?%3CTCOML%20version=%22NEWFORMAT%22%3E%3CTransferOnly%3E%3CAvailability%3E%3CRequest%3E%3CAgentCode%3ETEST%3C/AgentCode%3E%3CAgentType%3ETA%3C/AgentType%3E%3CDeparturePointCode%3EALC%3C/DeparturePointCode%3E%3CDeparturePointType%3EAIRPORT%3C/DeparturePointType%3E%3CArrivalPointCode%3EBEN%3C/ArrivalPointCode%3E%3CArrivalPointType%3ERESORT%3C/ArrivalPointType%3E%3CSectorType%3ERETURN%3C/SectorType%3E%3CArrDate%3E10.10.10%3C/ArrDate%3E%3CArrTime%3E10:00%3C/ArrTime%3E%3CRetDate%3E17.10.10%3C/RetDate%3E%3CRetTime%3E10:00%3C/RetTime%3E%3CBrochure%3E001%3C/Brochure%3E%3CAdults%3E2%3C/Adults%3E%3CChildren%3E0%3C/Children%3E%3CInfants%3E0%3C/Infants%3E%3CCurrencyCode%3EUKL%3C/CurrencyCode%3E%3C/Request%3E%3C/Availability%3E%3C/TransferOnly%3E%3C/TCOML%3E
You're looking for the WebClient class.
For example: (2nd EDIT: With GZIP; this code is tested and actually works)
string response;
using (var client = new WebClient()) {
byte[] bytes = client.DownloadData(url);
using(var reader = new StreamReader(new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress)))
response = reader.ReadToEnd();
}
However, if the URL returns raw XML, you can also load the XML directly from the URL, like this:
var doc = XDocument.Load(url);