得到错误:无法识别的元素'成长'。在阅读配置文件时

时间:2014-06-04 13:57:05

标签: c# .net xml

这是App.Config文件:

<configuration>

  <configSections>
    <section name="StartupFolders" type="ConfigSectionTester.StartupFoldersConfigSection, ConfigSectionTester"/>
  </configSections>

  <StartupFolders nick ="abc">

    <Folder>                                    
      <grow reader-type="PBV" />
      <add folderType="A" path="C:\foo" />    
      <add folderType="B" path="B:\foo1" />
      <add folderType="C"  />
    </Folder>

    <Files nikkie ="deaf">
      <add fileType="File 1" />  
    </Files>

  </StartupFolders>

</configuration>   

这些是我的课程:(专注于“成长”,因为这实际上是在创造问题)

namespace ConfigSectionTester
{
    class Program
    {
        static void Main(string[] args)
        {
            StartupFoldersConfigSection section = (StartupFoldersConfigSection)ConfigurationManager.GetSection("StartupFolders");

            if (section != null)
            {
                System.Diagnostics.Debug.WriteLine(section.FolderItems[0].FolderType);
                System.Diagnostics.Debug.WriteLine(section.FolderItems[0].Path);

                using (StreamWriter writer = new StreamWriter(@"D:\Sadiq\Sadiq.txt"))
                {
                    try
                    {
                        writer.WriteLine(section.FolderItems[1].FolderType);
                        writer.WriteLine(section.Nick);
                        writer.WriteLine(section.FileItems.Nikkie);
                        writer.WriteLine(section.FolderItems[1].Path);
                        writer.WriteLine(section.FolderItems[2].Path);
                        writer.WriteLine(section.FileItems[0].FileType);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(typeof(ConfigSectionTester.StartupFoldersConfigSection) + ":" + ex.TargetSite + ex.Message);
                    }

                }
            }
        }
    }

    public class FolderElement : ConfigurationElement
    {    
        [ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string FolderType
        {
            get
            {
                return ((string)(base["folderType"]));
            }
            set
            {
                base["folderType"] = value;
            }
        }

        [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Path
        {
            get
            {
                return ((string)(base["path"]));
            }
            set
            {
                base["path"] = value;
            }
        }
    }

    public class FileElement : ConfigurationElement
    {
        [ConfigurationProperty("fileType", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string FileType
        {
            get
            {
                return ((string)base["fileType"]);
            }
            set
            {
                base["fileType"] = value;
            }
        }
    }

    [ConfigurationCollection(typeof(FileElement))]
    public class FileCollection : ConfigurationElementCollection
    {
        [ConfigurationProperty("nikkie", DefaultValue = "")]
        public string Nikkie
        {
            get { return (string)this["nikkie"]; }
            set { this["nikkie"] = value; }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new FileElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FileElement)element).FileType;
        }
        public FileElement this[int ind]
        {
            get
            {
               //   return (FileElement)this[ind];
                return (FileElement)BaseGet(ind);
            }
        }
    }

    [ConfigurationCollection(typeof(FolderElement))]
    public class FoldersCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FolderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FolderElement)(element)).FolderType;
        }

        public FolderElement this[int idx]
        {
            get
            {
                return (FolderElement)BaseGet(idx);
            }
        }
    }

    public class Grow : ConfigurationElement
    {
        [ConfigurationProperty("reader-type")]
        public string ReaderType
        {
            get
            {
                return ((string)base["reader-type"]);
            }
            set
            {
                base["reader-type"] = value;
            }
        }
    }

    public class StartupFoldersConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("nick", DefaultValue = "8.5")]
        public string Nick
        {
            get { return (string)this["nick"]; }
            set { this["nick"] = value; }
        }

        [ConfigurationProperty("Folder")]
        public FoldersCollection FolderItems
        {
            get
            {
                return ((FoldersCollection)(base["Folder"]));
            }
        }

        [ConfigurationProperty("Files")]
        public FileCollection FileItems
        {
            get
            {
                return ((FileCollection)(base["Files"]));
            }
        }

        [ConfigurationProperty("grow")]
        public Grow GrowAtt
        {
            get { return (Grow)base["grow"]; }
        }
    }
}

在我添加之前,每件事都运转良好:

 <grow reader-type="PBV" />

我收到了以下错误:

无法识别的元素'成长'。

我是新手。我做错了什么?

0 个答案:

没有答案