Avatar of ITsolutionWizard
ITsolutionWizard
Flag 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

XMLC#ASP.NET

Avatar of undefined
Last Comment
Jonathan Dahan

8/22/2022 - Mon
ITsolutionWizard

ASKER
any helps?
ASKER CERTIFIED SOLUTION
louisfr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ste5an

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.
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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jonathan Dahan

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.