将xml反序列化为类,麻烦list<> [英] Deserializing xml to class, trouble with list<>

查看:86
本文介绍了将xml反序列化为类,麻烦list<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML

<map version="1.0">
    <properties>
        <property name="color" value="blue" />
        <property name="size" value="huge" />
        <property name="texture" value="rugged" />
    </properties>
</map>

我正在尝试编写可反序列化为的类,这就是我所拥有的:

I am trying to write classes that I can deserialize this into, this is what I have:

[XmlRoot("map")]
public class MyMap
{
    [XmlAttribute("version")]
    public decimal Version { get; set; }
    [XmlElement("properties")]
    public List<MyProperty> Properties { get; set; }
}

public class MyProperty
{
    [XmlAttribute("name")]
    public string Name { get; set; }
    [XmlAttribute("value")]
    public string Value { get; set; }
}

问题是我似乎无法阅读财产清单,我只是

The problem is that I cant seem to read the property list, I just get one entry and it has null in both Name and Value.

是否需要设置一些魔术属性才能使其生效?

Are there some magic attributes I need to set to get this to work?

推荐答案

您应如下更改MyMap。 XmlArray XmlArrayItem 是神奇的属性

You should change MyMap as below. XmlArray and XmlArrayItem are the magic attributes

[XmlRoot("map")]
public class MyMap
{
    [XmlAttribute("version")]
    public decimal Version { get; set; }
    [XmlArray("properties")]
    [XmlArrayItem("property")]
    public List<MyProperty> Properties { get; set; }
}

这篇关于将xml反序列化为类,麻烦list&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆