How to read the data of a.xml file and display in a textbox

Asked

Viewed 9,326 times

3

I have an application that generates an XML file, but I’m not able to read the XML file and insert it in Textbox.

 <?xml version="1.0"?>
 <parametros>
  <banco>rango</banco>
  <caminho>C:\</caminho>
 </parametros>

I need the application to read this xml and "import" the information to a textbox.

  • Well, there’s not trying to send for a TextBox, have you ever tried to put in it? Is there a problem reading XML? Which? Your code is doing just that. Maybe you have two separate difficulties.

  • Sorry, it’s because it has a textbox and a combobox but the two take the text and not the Dice.

  • I could put the xml and indicate more details so we can help.

  • Edited question @Diegosantos

  • @Brunorodrigues Ideally you should also leave the previous encoding in C#. Check if this is what you want!

2 answers

2


The answer is this:`

 XmlTextReader x = new XmlTextReader(@".\\SS-BACKUP.xml");

            while (x.Read())
            {
                if (x.NodeType == XmlNodeType.Element && x.Name == "banco")
                    cb_Banco.Text = (x.ReadString());
                if (x.NodeType == XmlNodeType.Element && x.Name == "caminho")
                    tb_Caminho.Text = (x.ReadString());
                if (x.NodeType == XmlNodeType.Element && x.Name == "servidor")
                    tb_Servidor.Text = (x.ReadString());
                if (x.NodeType == XmlNodeType.Element && x.Name == "usuario")
                    tb_Usuario.Text = (x.ReadString());
                if (x.NodeType == XmlNodeType.Element && x.Name == "senha")
                    tb_Senha.Text = (x.ReadString());
            }

            x.Close();
            return;

1

Using as a basis what you had, added assigns a + textbox that will work as an append.

XmlTextReader reader = new XmlTextReader("C:\\Users\\diego-santos\\Desktop\\teste.xml");

while (reader.Read())
{
  if (reader.NodeType == XmlNodeType.Text)
  {
      tb_Caminho.Text += reader.Value;
  }
}

the result = rangoC:\

Browser other questions tagged

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