質問

I'm starting to program in C # and i am beginner, so i don't have experience. I want to one day be a professional and started to develop solutions. My program save the information in xml file and then read the same information in same xml. The xml file has this format

<Dados>
  <Nome>Vitor Emanuel Macedo Ferreira</Nome>
  <Sexo>M</Sexo>
  <Idade>22</Idade>
  <Peso>86</Peso>
  <Altura>1.87</Altura>
</Dados>

And in C# code my solution has:

OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "XML|*.xml";
        ofd.FileName = ("c:\\xml\\data.xml");
        if (ofd.ShowDialog() ==  DialogResult.OK)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(ofd.FileName);
            xDoc.SelectSingleNode("Dados");
            if (ofd.FileName == "c:\\xml\\data.xml" &&  xDoc.SelectSingleNode(string.Empty) == xDoc.SelectSingleNode("Dados"))
            {


                    label8.Show();
                    textBox1.Hide();
                    textBox2.Hide();
                    textBox3.Hide();
                    radioButton1.Hide();
                    radioButton2.Hide();
                    label1.Hide();
                    label2.Hide();
                    label3.Hide();
                    label4.Hide();
                    label5.Hide();



            }
            else if (ofd.FileName == "c:\\xml\\data.xml" && xDoc.SelectSingleNode("") != xDoc.SelectSingleNode("Dados"))
            {
                MessageBox.Show("XML in incorrect path please put your xml file in c:\\xml");
            }



            }

How can I filter the content of the xml file, especially the tag . I need that my solution read the xml file and when he read the tag he should be able to say through Messagebox "Error tag is not equal to ", otherwise if tag equals to he must to continue

役に立ちましたか?

解決

Check this:

 XmlDocument doc = new XmlDocument();
 doc.Load(ofd.Filename);

Now use XmlNode to traverse through nodes:

 XmlNode rootNode = doc.SelectSingleNode("Dados");

Retrieve other nodes as well in this manner:

 XmlNode nomeNode = rootNode.SelectSingleNode("Nome");
 XmlNode saxoNode = rootNode.SelectSingleNode("Saxo");

This should give you a start.

他のヒント

You want to make use of XPath to navigate your XML document.

There's a guide on using it in C# here.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top