0

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

4

2 回答 2

1

检查这个:

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

现在使用 XmlNode 遍历节点:

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

也以这种方式检索其他节点:

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

这应该给你一个开始。

于 2013-08-23T21:36:53.327 回答
1

您想使用XPath来导航您的 XML 文档。

这里有一个在 C# 中使用它的指南

于 2013-08-23T21:25:20.760 回答