如何反序列化具有可变数量属性的 xml 元素?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
然后你的课程:)
没有经过检查,但它通常可以工作
try this :
Then your classes :)
It's not checked but it'll normally works