解析XML无法进入类

时间:2013-12-16 19:38:04

标签: c# xml serialization

我正在使用它:

namespace FTPUploader
{
    [XmlRoot("settings")]
    public class Settings
    {
        [XmlElement("source")]
        public Source[] Sources;
    }

    public class Source
    {
        [XmlElement("indetifier")]
        public string Indetifier;

        [XmlElement("directory")]
        public string Directory;

        [XmlElement("ftpsite")]
        public string FTPSite;

        [XmlElement("encrypt")]
        public string ShouldEncrypt;

        [XmlElement("zip")]
        public string shouldZip;

        [XmlArrayItem("specifier", typeof(Specifier))]
        [XmlElement("inclusions")]
        public Specifier[] Inclusions;

        [XmlArrayItem("specifier", typeof(Specifier))]
        [XmlElement("exclusions")]
        public Specifier[] Exclusions;

    }


    public class Specifier
    {
        [XmlText]
        public string Value;
    }
}

解析这个XML:

<settings>
  <source>
    <indetifier>1040</indetifier>
    <directory>C:\1040</directory>
    <ftpsite>ftp://corecard.com/TEstRegion/</ftpsite>
    <encrypt>yes</encrypt>
    <zip>yes</zip>
    <inclusions>
      <specifier>*.txt</specifier>
      <specifier>*.pdf</specifier>
      <specifier>*.xls</specifier>
    </inclusions>
    <exclusions>
    </exclusions>
  </source>
</settings>

使用:

            Settings settings;
            using (FileStream reader = new FileStream("info.xml", FileMode.Open))
            {
                XmlSerializer ser = new XmlSerializer(typeof(Settings));
                settings = ser.Deserialize(reader) as Settings;
            }

但它失败了。请让我知道我错在哪里。

1 个答案:

答案 0 :(得分:1)

使用XmlArray属性代替XmlElement

[XmlArray("inclusions")]
[XmlArrayItem("specifier", typeof(Specifier))]
public Specifier[] Inclusions;

[XmlArray("exclusions")]
[XmlArrayItem("specifier", typeof(Specifier))]
public Specifier[] Exclusions;