C# XML反序列化不在数组容器中的数组
我有一个XML文件(用于平铺平铺编辑器):(XML片段)C# XML反序列化不在数组容器中的数组,c#,arrays,xml,xmlserializer,C#,Arrays,Xml,Xmlserializer,我有一个XML文件(用于平铺平铺编辑器):(XML片段) 有人知道这是否可行,或者我必须求助于XMLReader吗?尝试将XMLMAP类重新定义为: [XmlRoot("map")] public class XMLMAP { [XmlAttribute("version")] public string Version; [XmlAttribute("orientation")] public string Orientation; [XmlAttrib
有人知道这是否可行,或者我必须求助于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;
}
并且可以看到map
包含2个TileSet
s的列表
请注意,我只能在向提供的xml末尾添加结束标记后才能执行此操作
希望对你有所帮助“地图”根本不是问题的根源问了这个问题后,我终于弄明白了,但非常感谢你成为真正回答这个问题的人@不用担心,我以前一直在努力解决这个问题,所以在正确理解它之前,我知道它可能会让人困惑
[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
}
}
[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;
}
var serializer = new XmlSerializer(typeof(XMLMAP));
var reader = new StreamReader(path);
var map = (XMLMAP)serializer.Deserialize(reader);
reader.Close();