Jack
Jack

Reputation: 3

Reading a part of an XML file using C sharp

I have a list of paths of files on an XML file. I need to show these path files on a data grid view. For now I have managed to show the exact number of rows as the same number of paths in the XML file but the paths do not show. If someone could show me how to show file paths on data grid views I would be grateful

private void button1_Click_1(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "XML|*.xml";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(ofd.FileName);

            foreach (XmlNode node in xDoc.SelectNodes("JobInfo/Folders/Folder"))
            {
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = node.InnerText;
            }

        }

    }

Upvotes: 0

Views: 66

Answers (1)

Abdul Saleem
Abdul Saleem

Reputation: 10622

Its not inner text, What you need is to specify the Attribute because you are using like Path="C:\..."

So Do like this

    if (ofd.ShowDialog() == DialogResult.OK)
    {
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(ofd.FileName);

        foreach (XmlNode node in xDoc.SelectNodes("JobInfo/Folders/Folder"))
            dataGridView1.Rows.Add(new object[]{node.Attributes["Path"].InnerText});

    }

Upvotes: 2

Related Questions