我正在使用它:
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;
}
但它失败了。请让我知道我错在哪里。
答案 0 :(得分:1)
使用XmlArray
属性代替XmlElement
:
[XmlArray("inclusions")]
[XmlArrayItem("specifier", typeof(Specifier))]
public Specifier[] Inclusions;
[XmlArray("exclusions")]
[XmlArrayItem("specifier", typeof(Specifier))]
public Specifier[] Exclusions;