nick_green
nick_green

Reputation: 1664

asp.net core can't get xml data from url

I'm using Amazon API to generate request for ItemLookup. After signing the request I got a url:

http://webservices.amazon.com/onca/xml?AWSAccessKeyId=[AccessKey]&AssociateTag=[AssociateTag]&ItemId=B06XBCY6DW&Operation=ItemLookup&ResponseGroup=Images%2CItemAttributes%2COffers%2CReviews&Service=AWSECommerceService&Timestamp=2050-02-13T12:00:00Z&Version=2016-02-12&Signature=[Signature]

Which I use in browser and I can see nice xml with item's data, but When I try to get that data with c# I get error

The Uri parameter must be a file system relative or absolute path.

The method I'm using to get data is:

var itemXml = XElement.Load(url);

How can I get that xml in c#??

Upvotes: 1

Views: 3142

Answers (1)

Alex
Alex

Reputation: 38529

This is nothing .net core specific.

XElement.Load accepts a string - which, if you look at the intellisense states:

Loads an XElement from a file.

This isn't what you want. It's trying to resolve the URI from the filesystem.

You'll need to read the URL content into a Stream.

Try the following

var httpClient = new HttpClient();
var result = httpClient.GetAsync(url).Result;

var stream = result.Content.ReadAsStreamAsync().Result;

var itemXml = XElement.Load(stream);

Note I've used .Result - I would recommend instead using await (async/await) pattern

Upvotes: 6

Related Questions