Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

xml call in c#

The Below code is working but I hope to have some improvement with the query string


Is it possible to have e.g. request.parm("SavedSchemaID","1")? 

I just hope to make the code cleaner.


 public string GetRawResult()
        {             string resultValue = string.Empty;             WebRequest request = WebRequest.Create("https://inc.com/masterfiles/customerV3/export.xml?SavedSchemaID=1&Entity=1&CustSeq=B&RecordLimit=20&RecordStartNum=1");                           request.Method = "GET";             request.Headers.Add("DevKey", "LWUoOiF5dFVXTSZdM0VpW1InNmI=");                         var response = request.GetResponse();             Stream receiveStream = response.GetResponseStream();             StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);             var result = readStream.ReadToEnd();             resultValue = result.ToString();                         return resultValue;         }    

Open in new window

Avatar of ITsolutionWizard
ITsolutionWizard
Flag of United States of America image

ASKER

any helps?
ASKER CERTIFIED SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In .NET Core you can use the QueryHelpers from Microsoft.AspNetCore.WebUtilities.

@Louis: It looks like you produce invalid URI's when there are fragments.
Avatar of louisfr
louisfr

@ste5an: Right. Using an UriBuilder is much better.
static class UriHelper
{
    public static Uri AddParameter(this Uri uri, string name, string value)
    {
        string connect = string.IsNullOrEmpty(uri.Query) ? $"?" : $"&";
        return new UriBuilder(uri) { Query = $"{uri.Query}{connect}{Uri.EscapeUriString(name)}={Uri.EscapeUriString(value)}" }.Uri;
    }
}

Open in new window

If you're calling a remote procedure using xml as the language to talk to the web service, you're better off adding a service reference to the web service which will let visual studio create an interface for you to talk to it. You can even consume an RPC library which uses xml as the language of communication. This is your best approach to make your code cleaner, good luck.