linguini
linguini

Reputation: 1939

illegal characters in path while reading xml(.rtdl)files in C# windows forms

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???

Upvotes: 0

Views: 2797

Answers (1)

yamen
yamen

Reputation: 15618

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
}

Upvotes: 2

Related Questions