【问题标题】:illegal characters in path while reading xml(.rtdl)files in C# windows forms在 C# windows 窗体中读取 xml(.rtdl) 文件时路径中的非法字符
【发布时间】:2012-05-10 07:26:11
【问题描述】:

在 Windows 窗体中,我在面板中有一些标签,我想显示来自 listBox1 的静态值,它从文件夹加载 (.rtdl) 文件的集合。

当用户选择每个时,我想在面板中显示相应的属性值到labels

填充 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);
            }
        }

从 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
                    }
                }
            }
        }

当我选择文件时,它会在using (XmlReader reader = XmlReader.Create(xmldoc)) 处抛出此错误illegal characters in path

请告诉我这里出了什么问题???

【问题讨论】:

    标签: c# xml visual-studio-2010 parsing xmlreader


    【解决方案1】:

    XmlReader.Create(string)path 作为输入(或流),而不是实际的文本字符串 - 请参见此处:http://msdn.microsoft.com/en-us/library/w8k674bf.aspx

    所以只需删除这一行:

    string xmldoc = File.ReadAllText(path);
    

    DisplayFile 中更改:

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

    到这里:

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

    也就是说,你正在以一种非常困难的方式做事。 LINQ to XML 对于您想要实现的目标要简单得多。

    改用DisplayFile 试试这个:

    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
    }
    

    【讨论】:

    • 我在这一行 string path = (string)listBox1.SelectedItem; 收到一条错误消息 Unable to cast object of type 'System.IO.FileInfo' to type 'System.String'.
    • 将该行更改为string path = file.FullName;
    • 标签中没有显示任何内容...!label5.Text = reader.Value;
    • 我认为我们已经解决了最初的问题,您可能在这里遇到了更大的问题。一方面,您正在读取属性,但您的数据在元素中。
    • @KarthikRANGARAJ 更改了我的答案,以展示如何重构该方法以实现您真正想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2011-05-20
    • 2018-02-28
    • 1970-01-01
    相关资源
    最近更新 更多