Mount Grid according to XML tags

1

I have a headache in my project here ...

I have to consume data from a webservice that sends them in xml format. Currently, I can get this xml from webservice , but I can not scan all the xml and mount my grid according to all tags which has in xml .

What happens is that xml has the root and several sub-elements that are not read. The xml layout is this:

<?xml version="1.0" encoding="UTF-8" ?>
<certidoes>
   <codigo_retorno>99999</codigo_retorno>
   <mensagem_retorno> </mensagem_retorno>
   <qtd_registros>0</qtd_registros>
<certidao>
   <codigo_hash />
   <metodo />
   <numero_solicitante />
   <numero_recebedor />
   <tipo_registro />
   <data_solicitacao />
   <nome_registrado_1 />
   <nome_registrado_2 />
   <novo_nome_registrado_1 />
   <novo_nome_registrado_2 />
   <data_ocorrido />
   <data_registro />
   <matricula />
   <obs_solicitacao />
   <emolumentos>0</emolumentos>
</certidao>
</certidoes>

What happens is that when creating the Grid with these tags, the Grid is created only with the first three tags, that is: <codigo_retorno> , <mensagem_retorno> and <qtd_registro> ... And the others are missing information ...

Is there any way I can do this Grid with all XML tags?

And how could I do that if the <qtd_registros> tag has value equal to 0, I do not show, say, ignore, and not show on the Grid? That is, show data that has value greater than 0.

The code I use to do this reading, I took the macoratti tutorial , where you have a button that has the following codes?

DataSet ds = new DataSet();
        ds.ReadXml(@"C:\caminho\do\arquivol");
        dgvXML.DataSource = ds.Tables[0].DefaultView;

And in my scenario I use the following form because the return of xml is dynamic:

DataSet tabela = new DataSet();
        MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(temp));
        tabela.ReadXml(ms);

How can I do this?

    
asked by anonymous 09.06.2015 / 14:49

1 answer

0

You can read XML as XDocument and convert it to an object as follows:

Define a class to represent XML values:

public class Certidao
{
    public string CodigoRetorno { get; set; }
    public string MensagemRetorno { get; set; }
    public string QtdRegistros { get; set; }
    public string CodigoHash { get; set; }
    public string Metodo { get; set; }
    public string NumeroSolicitante { get; set; }
    public string NumeroRecebedor { get; set; }
    public string TipoRegistro { get; set; }
    public string DataSolicitacao { get; set; }
    public string NomeRegistrado1 { get; set; }
    public string NomeRegistrado2 { get; set; }
    public string NovoNomeRegistrado1 { get; set; }
    public string NovoNomeRegistrado2 { get; set; }
    public string DataOcorrido { get; set; }
    public string DataRegistro { get; set; }
    public string Matricula { get; set; }
    public string ObsSolicitacao { get; set; }
    public string Emolumentos { get; set; }
}

Read using class XDocument :

using System.Xml.Linq;
....

XDocument doc = XDocument.Parse(xml);
List<Certidao> certidoes = new List<Certidao>();

foreach (var elmCertidao in doc.Root.Elements("certidao"))
{
    Certidao certidao = new Certidao()
    {
        CodigoRetorno = doc.Root.Element("codigo_retorno").Value,
        MensagemRetorno = doc.Root.Element("mensagem_retorno").Value,
        QtdRegistros = doc.Root.Element("qtd_registros").Value,
        CodigoHash = elmCertidao.Element("codigo_hash").Value,
        Metodo = elmCertidao.Element("metodo").Value,
        NumeroSolicitante = elmCertidao.Element("numero_solicitante").Value,
        NumeroRecebedor = elmCertidao.Element("numero_recebedor").Value,
        TipoRegistro = elmCertidao.Element("tipo_registro").Value,
        DataSolicitacao = elmCertidao.Element("data_solicitacao").Value,
        NomeRegistrado1 = elmCertidao.Element("nome_registrado_1").Value,
        NomeRegistrado2 = elmCertidao.Element("nome_registrado_2").Value,
        NovoNomeRegistrado1 = elmCertidao.Element("novo_nome_registrado_1").Value,
        NovoNomeRegistrado2 = elmCertidao.Element("novo_nome_registrado_2").Value,
        DataOcorrido = elmCertidao.Element("data_ocorrido").Value,
        DataRegistro = elmCertidao.Element("data_registro").Value,
        Matricula = elmCertidao.Element("matricula").Value,
        ObsSolicitacao = elmCertidao.Element("obs_solicitacao").Value,
        Emolumentos = elmCertidao.Element("emolumentos").Value
    };

    certidoes.Add(certidao);

}

gridView.DataSource = certidoes;
gridView.DataBind();

In this case, all data will be returned as String . I recommend doing the right conversions for each type, for example:

QtdRegistros = Convert.ToInt32(doc.Root.Element("qtd_registros").Value),
DataSolicitacao = Convert.ToDateTime(elmCertidao.Element("data_solicitacao").Value),

However, it will be necessary to treat case by case to avoid errors with null or empty values.

I did a NetFiddle with an example of reading.

    
09.06.2015 / 16:42