在xml中反序列化嵌套列表

本文关键字:嵌套 列表 反序列化 xml | 更新日期: 2023-09-27 18:01:36

我是c# silverlight初学者,我的情况是我有以下xml代码:

        string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> 
                           <par>  
                           <name>amount</name>
                           <label>Amount</label>
                           <unit>Hundred</unit >
                           <comp>
                           <type>Combo</type>
                           <attributes>
                           <type>Integer</type>
                           <displayed>4</displayed>
                           <selected>0</selected>
                           <item>5</item>       
                           </attributes>
                           </comp>
                           </par>";

现在是什么问题在最后一行中,当我尝试调试"项目"时,它被分配了几个值,如5在线debug . writeline (attribute .item);我只看到"5"在调试时它不显示其他值。我想我需要为它实现一个列表。但我不知道该怎么做。有人可以帮助我,这样我就能够在这个c#代码中有所有分配给item的值,因为在这一步之后,我必须创建一个GUI。那可帮了大忙了。

注意:我不能使用数组列表,因为silverlight不支持任何其他lasttnative请?

在xml中反序列化嵌套列表

这就是你需要的:

[XmlRoot(ElementName = "parameter")]
public class Parameter
{
    [XmlElement("name")]
    public string Name { get; set; }
    [XmlElement("label")]
    public string Label { get; set; }
    [XmlElement("unit")]
    public string Unit { get; set; }
    [XmlElement("component")]
    public Component Component { get; set; }
}
[XmlRoot(ElementName = "component")]
public class Component {
    [XmlElement("type")]
    public string Type { get; set; }
    [XmlElement("attributes")]
    public Attributes Attributes { get; set; }
}
[XmlRoot(ElementName = "attributes")]
public class Attributes
{
    [XmlElement("type")]
    public string Type { get; set; }
    [XmlElement("displayed")]
    public string Displayed { get; set; }
    [XmlElement("selected")]
    public string Selected { get; set; }
    [XmlArray("items")]
    [XmlArrayItem("item", typeof(string))]
    public List<string> Items { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        string xmlstring = @"<?xml version='1.0' encoding='utf-8' ?> 
                       <parameter>  
                       <name>max_amount</name>
                       <label>Max Amount</label>
                       <unit>Millions</unit>
                       <component>
                       <type>Combo</type>
                       <attributes>
                       <type>Integer</type>
                       <displayed>4</displayed>
                       <selected>0</selected>
                       <items>
                       <item>5</item>
                       <item>10</item>
                       <item>20</item>
                       <item>50</item>
                       </items>
                       </attributes>
                       </component >
                       </parameter>";

        XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));
        XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
        Parameter parameter = (Parameter)deserializer.Deserialize(reader);
        Console.WriteLine("Type: {0}", parameter.Component.Attributes.Type);
        Console.WriteLine("Displayed: {0}", parameter.Component.Attributes.Displayed);
        Console.WriteLine("Selected: {0}", parameter.Component.Attributes.Selected);
        Console.WriteLine("Items: ");
        foreach (var item in parameter.Component.Attributes.Items)
        {
            Console.WriteLine("'t{0}", item);
        }
        Console.ReadLine();
    }
}

注意xmlstring的小变化,现在每个<item></item>都在容器内:

<items>
    <item>5</item>
    <item>10</item>
    <item>20</item>
    <item>50</item>
</items>