Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用可变数量的属性反序列化xml元素?_Xml_Serialization - Fatal编程技术网

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

如何使用可变数量的属性反序列化xml元素?,xml,serialization,Xml,Serialization,此解决方案不起作用-我将所有对象都设为空(没有例外)。问题是,正如您从XML文件中看到的,元素-设置-具有数量可变的属性。我试图将它们表示为属性数组,但我猜这种表示是不正确的。。。有什么办法吗?试试这个: [Serializable()] [XmlRoot(Namespace = "", ElementName = "appconfigs", IsNullable = true)] public class AppConfigs { private AppConfig[] m_AppCo

此解决方案不起作用-我将所有对象都设为空(没有例外)。问题是,正如您从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; }
    }
}
然后你的班级:) 它没有被检查,但正常工作

[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; }
    }
}
[XmlRoot()]
public class ConfigurationFile
{
    private AppConfigs config = null;

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