samedi 10 avril 2021

Calling XML/JSON data from quickwatch in C# and displaying it on a webpage

I'm using an API to call some data into my website that's in MVC. Below is my code where I call the API (this is all working perfectly).

 public ActionResult Index(RenderModel model, string serviceNumber, string serviceCode)
    {
        string URL = @"****************************";

        string appid = @"*******";
        string appkey = @"***************************";

        string requestbody = @"<Siri xmlns=""***********"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" version=""2.0"" xsi:schemaLocation=""*********** *******************************"" >";
        requestbody += @"<ServiceRequest> <RequestTimestamp>2020-02-18T15:37:06+0000</RequestTimestamp> <RequestorRef>d0ab77fe</RequestorRef> <SituationExchangeRequest version=""2.0""> <RequestTimestamp>2020-06-05T15:51:06+0000</RequestTimestamp> </SituationExchangeRequest> </ServiceRequest> </Siri>";

        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, URL)
        {
            Method = HttpMethod.Post,
            RequestUri = new System.Uri(URL),
            Content = new StringContent(requestbody)
        };
        requestMessage.Headers.Add("Accept", "application/xml");
        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{appid}:{appkey}")));

        HttpClient client = new HttpClient();
        HttpResponseMessage response = client.SendAsync(requestMessage).GetAwaiter().GetResult();
        response.EnsureSuccessStatusCode();
        string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

        XmlSerializer serializer = new XmlSerializer(typeof(Siri));
        StreamReader reader = new StreamReader(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
        Siri SiriFeed = (Siri)serializer.Deserialize(reader);

when I run the code and put a breakpoint in I can right click the SiriFeed and click 'quickwatch' and view the data in xml format, how do I then extract the data from the quick watch and display it on my site (this API stuff is all new to me). for example This image displays the columns in quick watch

I want to be able to pull the data out from the situations column you'll see in the image, if you click the situations there's actually 20 or so different ones, I want to be able to pull that data out, loop through however many of them there is and then display it on my page. Please can I have a bit of guidance or knowledge/examples of anyone working with something similar - thanks!.

Aucun commentaire:

Enregistrer un commentaire