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 ?
Dude, this line is wrong: Xdocument doc = Xdocument.Load(new Memorystream(System.Text.Encoding.ASCII.Getbytes(temp)))...
– Érik Thiago
What error? If the temp variable is a string with your XML, you can use
XDocument doc = XDocument.Parse(xml);
. I copied that onenew Memory Stream
of your code, assuming the variabletemp
is a string with XML.– Marcus Vinicius
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...
– Érik Thiago
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
– Marcus Vinicius
Ah soy. Will have several tags
<certidao>
in one xml only. It worked, but when you get there inobject
of the null reference error.– Érik Thiago
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.– Marcus Vinicius
The helmet can not be used... Ta accusing that it can not be used here.
– Érik Thiago
Let’s go continue this discussion in chat.
– Marcus Vinicius