3
In my project, I need to select an XML file from some hard drive folder and then read its contents and show in a gridview.
Right now, I can open the window to select the file, but I can’t read that file and iterate over it and display the XML data in a gridview...
How can I do ?
My codes:
Stream myStream = null;
OpenFileDialog openFileDialogXML = new OpenFileDialog();
openFileDialogXML.InitialDirectory = "c:\\";
openFileDialogXML.Filter = "xml files (*.xml)|*.txt|All files (*.*)|*.*";
openFileDialogXML.FilterIndex = 2;
openFileDialogXML.RestoreDirectory = true;
if (openFileDialogXML.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialogXML.OpenFile()) != null)
{
using (myStream)
{
string caminhoArquivo = openFileDialogXML.ToString();
XmlDocument documento = new XmlDocument();
documento.Load(caminhoArquivo);
string docLoaded = documento.ToString();
// Insert code to read the stream here.
//classe que representa um xml, e faz o parse da string para o arquivo
XDocument doc = XDocument.Parse(docLoaded);
//cria a representação de uma tabela
DataTable tabela = new DataTable();
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
tabela.Columns.Add("tabela", typeof(string));
////cria as linhas da tabela
DataRow registro;
////itera sobre a tag do xml para pegar todos os dados
foreach (var elmCertidao in doc.Root.Elements("tag"))
{
//recebe os registros do xml e representa em linhas
registro = tabela.NewRow();
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
// //objeto com todas as colunas
tabela.Rows.Add(registro);
}
////monta o grid com as informações presentes no objeto
dgDataGrid.DataSource = tabela;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
I know this is wrong, but I’ve never done this kind of thing, so if you could help me, it would be really nice!
EDIT
I am able to scan the XML, but only up to a certain tag... How can I get everything ? I can only scan up to the tag 13.
Codes:
if (openFileDialogXML.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialogXML.OpenFile()) != null)
{
using (myStream)
{
//limpa o conteudo do textbox
txtCaminhoArquivoXML.Text = "";
// Le os arquivos selecionados
foreach (String arquivo in openFileDialogXML.FileNames)
{
txtCaminhoArquivoXML.Text += arquivo;
}
try
{
// carrega e exibe o arquivo XML(le o conteudo do xml que esta no caminho do diretório)
StreamReader SR = File.OpenText(txtCaminhoArquivoXML.Text);
string _stringXml = SR.ReadToEnd();
SR.Close();
_stringXml = _stringXml.Replace("\n", "");
_stringXml = _stringXml.Replace("\r", "");
//faz o parse de uma string para uma representação de XML
XDocument doc = XDocument.Parse(_stringXml);
//cria a representação de uma tabela
DataTable tabela = new DataTable();
tabela.Columns.Add("tag1", typeof(string));
tabela.Columns.Add("tag2", typeof(string));
tabela.Columns.Add("tag3", typeof(string));
tabela.Columns.Add("tag4", typeof(string));
tabela.Columns.Add("tag5", typeof(string));
tabela.Columns.Add("tag6", typeof(string));
tabela.Columns.Add("tag7", typeof(string));
tabela.Columns.Add("tag8", typeof(string));
tabela.Columns.Add("tag9", typeof(string));
tabela.Columns.Add("tag10", typeof(string));
tabela.Columns.Add("tag11", typeof(string));
tabela.Columns.Add("tag12", typeof(string));
tabela.Columns.Add("tag13", typeof(string));
tabela.Columns.Add("tag14", typeof(string));
tabela.Columns.Add("tag15", typeof(string));
tabela.Columns.Add("tag16", typeof(string));
tabela.Columns.Add("tag17", typeof(string));
tabela.Columns.Add("tag18", typeof(string));
tabela.Columns.Add("tag19", typeof(string));
tabela.Columns.Add("tag20", typeof(string));
//cria as linhas da tabela
DataRow registro;
//itera sobre a tag do xml para pegar todos os dados
foreach (var elmCertidao in doc.Root.Elements("emissao"))
{
//recebe os registros do xml e representa em linhas
registro = tabela.NewRow();
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(elmCertidao.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
registro["tag"] = GetValueOrDefault(doc.Root.Element("tag"));
//objeto com todas as colunas
tabela.Rows.Add(registro);
}
//monta o grid com as informações presentes no objeto
dgDataGrid.DataSource = tabela;
}
catch (Exception ex)
{
MessageBox.Show("Erro ao carregar arquivo : " + ex.Message);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Não foi possível ler o arquivo a partir do disco. Erro original: " + ex.Message);
}
}
Does it give any kind of error? If yes, which one? If you give a Databind() on the Grid, it shows something?
– pnet
Simply does not mount the grid. I cannot load the xml content into the variable
XDocument doc = XDocument.Parse(docLoaded);
.– Érik Thiago
Have you tried inspecting
doc
? What appears in the properties?– Leonel Sanches da Silva
@Érikthiago, in your code there is no Bind in the grid. You should give dgDataGrid.Databind(), after the line where you define the Datasource of the same.
– pnet
So, now I can load the xml data, but it doesn’t show all the data that has in it. Only the data that is in the first one appears, and not in the others. = (. Can I put my updated code ? And see if you help me ?
– Érik Thiago
@pnet and Gypsy, I managed to solve! Thanks for your attention. I even answered my question to help other people!
– Érik Thiago