2013-12-16 91 views
-2

我使用這個:解析失敗XML爲

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

但它失敗。請讓我知道我錯在哪裏。

+2

'但fails.'它是如何失敗?你有例外嗎?如果是,那麼錯誤信息是什麼?或意外的結果?怎麼樣? –

回答

1

使用XmlArray屬性,而不是XmlElement

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

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

正確的目標! –