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

发布于 2024-12-16 20:09:49 字数 4522 浏览 0 评论 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# 类看起来像这样:

[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- 具有可变数量的属性。我试图将它们表示为属性数组,但我想这种表示是不正确的......我有什么想法吗?

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

My C# classes look like that:

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

This solution does not work - I get all object as nulls (no exceptions). The problem is, as you can see from XML file is that the element -settings- has a variable number of attributes. I tried to represent them as array of attributes but I guess this representation is incorrect... Any ideas how I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-12-23 20:09:50

试试这个:

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

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

然后你的课程:)
没有经过检查,但它通常可以工作

try this :

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

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

Then your classes :)
It's not checked but it'll normally works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文