What a good day, I've seen many examples of how to read xml files, but they do it from the path of the file. However, I want to search the xml files. with the OpenFileDialog (It should be noted that the xml have the same structure, the nodes do not change only the data). This is my example of my xml file:
Now I will show you my code what I am trying to do:
private void btn3_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.InitialDirectory = "C:\Users\Prueba";
OpenFileDialog1.Filter = "xml files (*.xml)|*.xml|Todos los archivos (*.*)|*.*";
OpenFileDialog1.FilterIndex = 2;
OpenFileDialog1.RestoreDirectory = true;
OpenFileDialog1.Multiselect = true;
if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = OpenFileDialog1.OpenFile()) != null)
{
XmlTextReader xmlTextReader = new XmlTextReader(""); //Aquí pondría la ruta de origen del archivo pero eso no es lo que yo quiero.
string etiquetafinal = "";
while (xmlTextReader.Read())
{
if (xmlTextReader.NodeType == XmlNodeType.Element)
{
rtxt1.Text += (new string(' ', xmlTextReader.Depth * 3) + "<" + xmlTextReader.Name + ">");
etiquetafinal = xmlTextReader.Name;
continue;
}
if (xmlTextReader.NodeType == XmlNodeType.Text)
{
rtxt1.Text += xmlTextReader.ReadContentAsString() + "</" + etiquetafinal + ">";
}
else
{
rtxt1.Text += "\r";
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
I hope you understand me, I want to find the xml file and read it. thanks.