The serialization does not work because your class members are private
and the xml serialization works only with public members. First change you need to make is to make your members, that describe the xml structure, public:
From docs.microsoft.com: Class members can have any of the five kinds of declared accessibility and default to private declared accessibility
.
public class Conciliation
{
[XmlElement("Header")]
public Header Header { get; set; }
}
public class Header
{
[XmlElement("GenerationDateTime")]
public string GenerationDateTime { get; set; }
[XmlElement("StoneCode")]
public int StoneCode { get; set; }
[XmlElement("LayoutVersion")]
public int LayoutVersion { get; set; }
[XmlElement("FileId")]
public int FileId { get; set; }
[XmlElement("ReferenceDate")]
public string ReferenceDate { get; set; }
}
The deserialization is actually pretty short. The example XML can be deserialized with the following code:
using (var fs = new FileStream(@"c:empxml1.xml", FileMode.Open))
{
var xmls = new XmlSerializer(typeof(Conciliation));
var xmlStructure = ((Conciliation)xmls.Deserialize(fs));
xmlStructure.Dump();
}
The parsed values looks like this:
Make a small method that deserializes a string input and use it in your BuscarConciliacaoDiaCliente()
.
For example:
private Conciliation deserializeXml(string xmlContent)
{
using (var fs = (TextReader)new StringReader(xmlContent))
{
var xmls = new XmlSerializer(typeof(Conciliation));
var conciliation = ((Conciliation)xmls.Deserialize(fs));
return conciliation;
}
}
Pass as xmlContent
the content of resposta.Content.ReadAsStringAsync().Result
.
I don't know your logic in detail, but IMO your function could be written as follows:
public string BuscarConciliacaoDiaCliente()
{
using (BDBONZAY bd = new BDBONZAY())
{
try
{
using (HttpClient requisicao = new HttpClient())
{
var resposta = requisicao.GetAsync("https://gabriel.free.beeceptor.com/bonzay").Result;
if (resposta.StatusCode == HttpStatusCode.OK)
{
//var xml = new XmlDocument();
//xml.LoadXml(resposta);
bd.BeginTransaction();
Conciliation result = deserializeXml(resposta);
//XmlSerializer serializer = new XmlSerializer(typeof(Conciliation));
//using (TextReader reader = new StringReader(resposta))
//{
// Conciliation result = (Conciliation)serializer.Deserialize(reader);
//}
bd.CommitTransaction();
}
}
}
catch (Exception ex)
{
bd.RollbackTransaction();
throw;
}
//}
return "";
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…