【问题标题】:deserializing a nested list in xml反序列化 xml 中的嵌套列表
【发布时间】:2014-05-05 08:50:17
【问题描述】:

我是 c# silverlight 初学者,我有 thios 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>";

** 现在有什么问题?** 在最后一行中,当我尝试调试“项目”时,它在 Debug.WriteLine(attrib.item) 上分配了几个值,例如 5;我只看到 "5" 调试它不显示其他值。我想我需要为它实现一个列表。但是我不知道如何在这里做到这一点。有人可以帮助我,这样我就可以在这个 c# 代码中为item 分配所有值,因为在这一步之后我必须创建它的 GUI。会有很大的帮助。

注意:我不能使用 ArrayList,因为 silverligth 不支持任何其他 lastertnative 吗?

【问题讨论】:

  • 有类似 [XmlArray][XmlArrayItem] 的东西,您应该检查并尝试。
  • @rosko 如果我想使用 list insted of arrays 怎么办?那我该怎么办?
  • 你的观点?这只是一种语法......下面的答案中的以下示例是否不适合您?

标签: c# xml silverlight deserialization xml-deserialization


【解决方案1】:

这是你需要的:

[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 中的小变化,现在每个 &lt;item&gt;&lt;/item&gt; 都在容器内:

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

【讨论】:

  • 感谢您的链接。但是是否可以使用 List insted of arrays 来做到这一点?
  • 您的观点...?它只是用于序列化和反序列化的注释。你可以用它序列化或反序列化列表。
  • 当我尝试这样做时它给出了异常。您能否根据您建议的链接编辑我的代码?非常感谢,将是一个很大的帮助。
  • 我在 attribute.cs " [XmlArrayItem("item", typeof(attributes))]" ad this in component.cs " [XmlArray("attributes")] public List属性 { 获取; 设置; } "
  • 错误 1 ​​找不到类型或命名空间名称“ArrayList”(您是否缺少 using 指令或程序集引用:“ [XmlArrayItem("item", typeof(attributes))] public ArrayList item { get; set; }" 我正在使用 silverlight-5 并且我已经包含了 "System.Collections" 即使它仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
相关资源
最近更新 更多