0

In windows forms, I have some labels in the panel and I would like to display the static values from the listBox1 where it loads collection of (.rtdl) files from a folder.

When user selects each then I want to display the corresponding attribute values to the labels in the panel.

Code to populate listBox1:

private void Form1_Load(object sender, EventArgs e)
        {
            PopulateListBox(listBox1, @"C:\TestLoadFiles\", "*.rtdl");
        }

        private void PopulateListBox(ListBox lsb, string Folder, string FileType)
        {
            DirectoryInfo dinfo = new DirectoryInfo(Folder);
            FileInfo[] Files = dinfo.GetFiles(FileType);
            foreach (FileInfo file in Files)
            {
                lsb.Items.Add(file);
            }
        }

Code to read files from the listBox1:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            FileInfo file = (FileInfo)listBox1.SelectedItem;
            DisplayFile(file.FullName);

            string path = (string)listBox1.SelectedItem;
            DisplayFile(path);
        }

        private void DisplayFile(string path)
        {
            string xmldoc = File.ReadAllText(path);

            using (XmlReader reader = XmlReader.Create(xmldoc))
            {

                while (reader.MoveToNextAttribute())
                {
                    switch (reader.Name)
                    {
                        case "description":
                            if (!string.IsNullOrEmpty(reader.Value))
                                label5.Text = reader.Value; // your label name
                            break;
                        case "sourceId":
                            if (!string.IsNullOrEmpty(reader.Value))
                                label6.Text = reader.Value; // your label name
                            break;
                        // ... continue for each label
                    }
                }
            }
        }

When I select the file, it's throwing this error illegal characters in path at using (XmlReader reader = XmlReader.Create(xmldoc)).

Please tell me what is wrong here???

linguini
  • 1,939
  • 5
  • 49
  • 79

1 Answers1

2

XmlReader.Create(string) takes a path as input (or a stream), not the actual text string - see here: http://msdn.microsoft.com/en-us/library/w8k674bf.aspx.

So just remove this line:

string xmldoc = File.ReadAllText(path);

And in DisplayFile change this:

using (XmlReader reader = XmlReader.Create(xmldoc))

To this:

using (XmlReader reader = XmlReader.Create(path))

That said, you're doing things in a very difficult way. LINQ to XML is way simpler for what you're trying to achieve.

Try this in DisplayFile instead:

private void DisplayFile(string path)
{
    var doc = XDocument.Load(path);
    var ns = doc.Root.GetDefaultNamespace();    
    var conn = doc.Root.Element(ns + "connection");

    label5.Text = conn.Element(ns + "description").Value;
    label6.Text = conn.Element(ns + "sourceId").Value;

    // and so on
}
yamen
  • 15,390
  • 3
  • 42
  • 52
  • I'm getting a error message `Unable to cast object of type 'System.IO.FileInfo' to type 'System.String'.` at this line `string path = (string)listBox1.SelectedItem;` – linguini May 10 '12 at 07:40
  • Change that line to `string path = file.FullName;` – yamen May 10 '12 at 07:43
  • Nothing is displaying in the label...!`label5.Text = reader.Value;` – linguini May 10 '12 at 07:55
  • I think we've solved the original problem, you probably have bigger issues here. For one, you're reading attributes but your data is in elements. – yamen May 10 '12 at 08:00
  • @KarthikRANGARAJ changed my answer to show how you could refactor that method to achieve what you really want. – yamen May 10 '12 at 08:13
  • can you tell me how can i proceed with `LINQ to XML`to read the xml files and display data??? – linguini May 10 '12 at 09:41
  • oh no, i have already tried that> I got an error ` XDocument.Load(path);` this is the error : `XDocument` doesn't exist in the current context – linguini May 10 '12 at 09:52