c# - XML反序列化不在数组容器中的数组

标签 c# arrays xml xmlserializer

我有一个 XML 文件(用于 Tiled 磁贴编辑器):(xml 片段)

<map version="1.0" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="32" tileheight="32" nextobjectid="1">
 <tileset firstgid="1" name="GrassyTile_01" tilewidth="32" tileheight="32" tilecount="4" columns="2">
  <image source="GrassyTile_01.png" width="64" height="64"/>
 </tileset>
 <tileset firstgid="5" name="BlackTile_01" tilewidth="32" tileheight="32" tilecount="1" columns="1">
  <image source="BlackTile_01.jpg" width="32" height="32"/>
 </tileset>

关于“tileset”元素,它们可以有 X 个,但它们不包含在 XmlArray 中,而只是一个接一个。

我正在寻找一种使用 XMLSerializer 将这些元素反序列化为数组的方法,但我找不到执行此操作的正确方法。

这是我的代码:

 [XmlRoot("map")]
        public class XMLMAP
        {
            [XmlAttribute("version")]
            public string Version;
            [XmlAttribute("orientation")]
            public string Orientation;
            [XmlAttribute("renderorder")]
            public string Renderorder;
            [XmlAttribute("width")]
            public int Width;
            [XmlAttribute("height")]
            public int Height;
            [XmlAttribute("tilewidth")]
            public int Tilewidth;
            [XmlAttribute("tileheight")]
            public int TileHeight;
            [XmlAttribute("nextobjectid")]
            public int NextObjectID;
            [XmlArray]
            public XMLMAP_TILESET[] TileSets;
            [XmlRoot("tileset")]
            public class XMLMAP_TILESET
            {
                [XmlAttribute("firstgid")]
                public string FirstGID;
                //No need for rest of code
            }
        }

有人知道这是否可行,或者我必须求助于 XMLReader 吗?

最佳答案

尝试重新定义您的 XMLMAP分类为:

[XmlRoot("map")]
public class XMLMAP
{
    [XmlAttribute("version")]
    public string Version;
    [XmlAttribute("orientation")]
    public string Orientation;
    [XmlAttribute("renderorder")]
    public string Renderorder;
    [XmlAttribute("width")]
    public int Width;
    [XmlAttribute("height")]
    public int Height;
    [XmlAttribute("tilewidth")]
    public int Tilewidth;
    [XmlAttribute("tileheight")]
    public int TileHeight;
    [XmlAttribute("nextobjectid")]
    public int NextObjectID;

    [XmlElement("tileset")]
    public List<TileSet> TileSets;
}

[XmlRoot("tileset")]
public class TileSet
{
    [XmlAttribute("firstgid")]
    public string FirstGID;
}

在哪里XMLMAP包含 List<TileSet> , 和 TileSet然后被定义为一个单独的类,其中包含所有必需的属性等。

我现在可以反序列化

var serializer = new XmlSerializer(typeof(XMLMAP));

var reader = new StreamReader(path);
var map = (XMLMAP)serializer.Deserialize(reader);
reader.Close();

并且可以看到map包含 2 TileSet 的列表

请注意,我只能在添加结束符后执行此操作 </map>标记到您提供的 xml 的末尾。

希望对你有帮助

关于c# - XML反序列化不在数组容器中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43835394/

相关文章:

arrays - 你调用的对象是空的

c# - 如果我们将它与结构对象一起使用,我们可以初始化一个数组吗? | C#

iphone - XML文件怎么写?

c# - 在 TeamCity 构建结果中显示 `dnu publish` 异常

c# - 结构类型数组的性能

通过 Objective-C 消息传递访问 C 风格对象数组的正确语法?

android - clearFocus on editText 在三星 Galaxy Tab A 上调用两次 onFocusChange (Android API 24)

java - 元素中的 xsd 验证抛出内容类型异常

c# - 无法在 EF Core : "42P07: relation "AspNetRoles"already exists"中使用迁移

c# - Skip/Take Missing 的 Lambda 过载