Mount Grid according to XML tags

Asked

Viewed 399 times

1

I have a headache in my project here...

I have to consume data from a webservice that sends them in format xml. At the present moment, I can receive this xml of webservice, but I can’t sweep the whole xml and mount my grid according to all the tags on xml.

What happens is that the 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, ie: <codigo_retorno>, <mensagem_retorno> and <qtd_registro>... And the other information is missing...

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

And how could I do to get that tag <qtd_registros> has a value of 0, I do not show, I mean ignore, and not show in Grid ? That is, show the data that has a value of more than 0.

The code I use to make this reading, I took the tutorial of macoratti, where there is 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 as follows, because it is dynamic the return of xml:

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

How can I do ?

1 answer

0


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

Set 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 the 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 making the appropriate 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 made a Netfiddle with an example of reading.

  • Dude, this line is wrong: Xdocument doc = Xdocument.Load(new Memorystream(System.Text.Encoding.ASCII.Getbytes(temp)))...

  • What error? If the temp variable is a string with your XML, you can use XDocument doc = XDocument.Parse(xml);. I copied that one new Memory Stream of your code, assuming the variable temp is a string with XML.

  • But that’s right, it’s a string with xml. The error that gives it expects an Xmlreader in the Xdocument parameter... And for example, if I need to scan multiple certificates in xml and show them one by one, in lines, how can I do it ? And another thing, if I do this I will be able to show all the tags ? Because from what I understand, only shows the tags of the certificate...

  • 1

    The XDocument.Parse(xml) worked? You will have several tags <certidao> in the same XML or will it have several XML’s with that same structure? Pay attention to the code that it is reading all tags. In the first three lines of the reading, I’m reading straight from the root, for example: doc.Root.Element("codigo_retorno"). Then I read from the tag <certidao> from a variable containing its reference: XElement elmCertidao = doc.Root.Element("certidao")...elmCertidao.Element("codigo_hash").Value

  • Ah soy. Will have several tags <certidao> in one xml only. It worked, but when you get there in object of the null reference error.

  • I changed the answer. I created a class to represent the XML values and read several elements <certidao>. Take a look here https://dotnetfiddle.net/4P5VvJ to see also working.

  • The helmet can not be used... Ta accusing that it can not be used here.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.