如何使用可变数量的属性反序列化xml元素?

时间:2011-11-18 05:19:52

标签: xml serialization

<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#类看起来像这样:

[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 个答案:

答案 0 :(得分:-1)

试试这个:

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

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

然后你的课程:) 它没有被检查但它通常会起作用