2011-11-18 64 views
0
<appconfigs> 
<appconfig id="voicemail"> 
    <account 
     account_id="1106" 
     username="333" 
     password="333333" 
     appserver="https://10.10.3.120/json" 
    /> 
    <settings use_read_cache="1" show_read_toggle="all" callback="333"/> 
</appconfig> 
<appconfig id="contacts"> 
    <account 
     account_id="1106" 
     username="333" 
     password="333333" 
     appserver="https://10.10.3.120/json" 
    /> 
    <settings use_local_storage="0" /> 
    <auto_start /> 
</appconfig> 
<appconfig id="status"> 
    <account 
     account_id="1106" 
     username="333" 
     password="333333" 
     appserver="https://10.10.3.120/json" 
    /> 
    <auto_start /> 
</appconfig> 
<appconfig id="parking"> 
    <account 
     account_id="1106" 
     username="333" 
     password="333333" 
     appserver="https://10.10.3.120/json" 
    /> 
</appconfig> 
<appconfig id="queues"> 
    <account 
     account_id="1106" 
     username="333" 
     password="333333" 
     appserver="https://10.10.3.120/json" 
    /> 
</appconfig> 
</appconfigs> 

我的C#類看起來像:如何使用可變數量的屬性反序列化xml元素?

[Serializable()] 
[XmlRoot(Namespace = "", ElementName = "appconfigs", IsNullable = true)] 
public class AppConfigs 
{ 
    private AppConfig[] m_AppConfigs; 
    [System.Xml.Serialization.XmlElementAttribute("appconfig", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public AppConfig[] AppConfigCollection 
    { 
     get { return m_AppConfigs; } 
     set { m_AppConfigs = value; } 
    } 
} 

[Serializable()] 
[XmlRoot(Namespace = "", ElementName = "appconfig", IsNullable = true)] 
public class AppConfig 
{ 
    private string id; 

    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Id 
    { 
     get { return id; } 
     set { id = value; } 
    } 

    private Account m_Account = new Account(); 

    [System.Xml.Serialization.XmlElementAttribute("account", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public Account Account 
    { 
     get { return m_Account; } 
     set { m_Account = value; } 
    } 

    private Settings m_Settings = new Settings(); 

    [System.Xml.Serialization.XmlElementAttribute("settings", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public Settings Settings 
    { 
     get { return m_Settings; } 
     set { m_Settings = value; } 
    } 

} 

[Serializable()] 
[XmlRoot(Namespace = "", ElementName = "settings", IsNullable = true)] 
public class Settings 
{ 
    private string[] m_sItems; 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string[] AppItemCollection 
    { 
     get { return m_sItems; } 
     set { m_sItems = value; } 
    } 

    //private AppItem[] m_AppItems; 

    //[System.Xml.Serialization.XmlAttributeAttribute()] 
    //public AppItem[] AppItemCollection 
    //{ 
    // get { return m_AppItems; } 
    // set { m_AppItems = value; } 
    //} 
}  

[Serializable()] 
[XmlRoot(Namespace = "", ElementName = "account", IsNullable = true)] 
public class Account 
{ 
    private string account_id; 
    private string username; 
    private string password; 
    private string appserver; 

    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string AccountId 
    { 
     get { return account_id; } 
     set { account_id = value; } 
    } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Username 
    { 
     get { return username; } 
     set { username = value; } 
    } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Password 
    { 
     get { return password; } 
     set { password = value; } 
    } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Appserver 
    { 
     get { return appserver; } 
     set { appserver = value; } 
    } 
} 

[Serializable()] 
[XmlRoot(Namespace = "", IsNullable = true)] 
public class AppItem 
{ 
    private string name; 
    private string val; 

    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string Val 
    { 
     get { return val; } 
     set { val = value; } 
    } 
} 

這個解決方案行不通 - 我得到的所有對象爲空(無例外)。問題是,正如您從XML文件中看到的那樣,元素-settings-具有可變數目的屬性。我試圖將它們表示爲屬性數組,但我想這種表示形式是不正確的...任何想法我怎麼做?

回答

-1

試試這個:

[XmlRoot()] 
public class ConfigurationFile 
{ 
    private AppConfigs config = null; 

    [XmlElement("appconfigs")] 
    public AppConfigs Config 
    { 
     get { return this.config ; } 
     set { this.config = value; } 
    } 
} 

然後你的類:) 它未被選中,但它會正常工作