Как десериализовать элемент xml с переменным количеством атрибутов? - PullRequest
0 голосов
/ 18 ноября 2011
<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 голосов
/ 30 ноября 2011

попробуйте это:

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

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

Тогда ваши классы :) Это не проверено, но будет нормально работать

...