Reputation: 1
Can you help me import this XML?
My difficulty is that he has no Elements.
Ex:
<?xml version="1.0" encoding="Windows-1252"?>
<TBL>
<CLI001>
<NrCli>1000</NrCli>
<Nome>Garagem Mira-Sintra, Lda.</Nome>
<CPost>2710-011 SINTRA</CPost>
<Comercial>7</Comercial>
<VndNome>A. Tomás & Lúcio- Terraplanagens e Cofragens, Lda</VndNome>
</CLI001>
<CLI002>
<NrCli>1001</NrCli>
<Nome>Auto Mecânica Torreense, Lda</Nome>
<CPost>2560-231 TORRES VEDRAS</CPost>
<Comercial>8</Comercial>
<VndNome>Ricardo Rui Ribeiro - Sociedade de advogados</VndNome>
</CLI002>
<CLI003>
<NrCli>1002</NrCli>
<Nome>Auto Peças de Miranda, Lda</Nome>
<CPost>5210-300 SÃO MARTINHO DE ANGUEIRA</CPost>
<Comercial>10</Comercial>
<VndNome>Jacinto Marques de Almeida Saraiva</VndNome>
</CLI003>
<TBL>
Only the first record is import:
try
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"C:\Users\Rui\Desktop\myfilename.xml");
dataGridView1.DataSource = dataSet.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
I can not see a way to get around this: s
Upvotes: 0
Views: 148
Reputation: 1859
Firstly, there's a typo in your example xml, it should have a closing tag. I'm assuming that your actual XML has the closing tag, since your code would have failed when it tried to load the XML into the DataSet.
The reason that you're only seeing one row in your datagridview is because there is only one row in dataset.Tables[0]. Each of your CLI tags (CLI001, CLI002, CLI003) are loaded as seperate tables into your dataset. Look at the structure of the data loaded in your datagrid view, it conforms to the child items of CLI001. If you want to display all of them in a single DataGridView, you will need to merge them into a single datatable.
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"C:\Users\Rui\Desktop\myfilename.xml");
//merge all the tables into a single one
var dtMerged = dataSet.Tables[0].Copy();
for (int i = 1; i < dataSet.Tables.Count; i++)
{
dtMerged.Merge(dataSet.Tables[i]);
}
//set the datasource using the merged datatable
dataGridView1.DataSource = dtMerged;
Upvotes: 0
Reputation: 124
Then end Tag <TBL>
should be closing tag </TBL>
. Next time you can use this URL to validate your xml.
Upvotes: 2