你好,我试图获取每个选项的值,如下所示。反序列化XML嵌套项
<Mailbox>
<Menus>
<Specs>
<Section>Message</Section>
<Options>
<Option>&New</Option>
<Option>&Inbox</Option>
<Option>&Outbox</Option>
<Option>&Sent Items</Option>
<Option>S&ettings</Option>
</Options>
</Specs>
</Menus>
</Mailbox>
但是当我运行下面的代码:
[XmlRoot("Mailbox")]
public class Mailbox
{
[XmlArray(ElementName = "Menus"), XmlArrayItem("Specs")]
public Specs[] Menus { get; set; }
}
[XmlRoot("Specs")]
public class Specs
{
[XmlElement("Section")]
public string Section { get; set; }
[XmlArray(ElementName = "Options"), XmlArrayItem("Option")]
public Options[] OptionsList { get; set; }
}
[XmlRoot("Options")]
public class Options
{
[XmlElement("Option")]
public string Option { get; set; }
}
var deserializer = new XmlSerializer(typeof(Mailbox));
var textReader = new StreamReader(filename);
try
{
var storage = (Mailbox)deserializer.Deserialize(textReader);
for (int i = 0; i < storage.Menus.Length; i++)
{
MainMenu.Section = storage.Menus[i].Section;
MainMenu.Options.Add(storage.Menus[i].Options[i]);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
textReader.Close();
我没有得到的选项;他们都只是空出来。 OptionsList没有正确显示,我不知道为什么。
谢谢。
变化atrribute上述选项阵列从XmlArrayItem [选项] XmlArrayItem [选项]? – brumScouse
在嵌套模型上,xmlroot有点困惑,以为只有一个,除非你编写这些eleswhere。 – brumScouse