What you want is actually a deserialization: transform an XML into an object that can be read and populate variables to persist in your database.
Below I detail a script that can serve your problem.
Reading the XML
This first step requires you to open a request for the site and read the response (which is XML). The code below does this:
using System.Net.Http;
var client = new HttpClient();
var uri = new Uri("http://www.bmfbovespa.com.br/Pregao-Online/ExecutaAcaoAjax.asp?CodigoPapel=BISA3|PETR4|PETR3");
HttpResponseMessage response = await client.GetAsync(uri);
Converting the response to XML
If response
is ok, we have to read XML. As your XML does not have namespace
, the reading procedure is simpler and can be done as follows:
if (response.IsSuccessStatusCode)
{
var responseString = response.Content.ReadAsStringAsync().Result;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(responseString);
var nodes = xmlDoc.SelectNodes("/ComportamentoPapeis/*");
foreach (XmlNode childNode in nodes)
{
switch (childNode.Name)
{
case "Papel":
// Coloque sua lógica de negócios aqui.
// A dica que dou pro seu caso é ler childNode.Attributes.
break;
}
}
If I need to, I’ll put a proof of concept on Github.
I don’t think it’s duplicate. He wants a deserialization, not necessarily read attribute to attribute, otherwise the answer indicated would perfectly meet.
– Leonel Sanches da Silva
@Ciganomorrisonmendez is actually the same question, only written differently.
– Bruno César
I was going to speak on the previous question too, but I thought the duplicate might help. As it did not solve, I put another answer.
– Leonel Sanches da Silva