简体   繁体   English

在 Xml 反序列化期间,矩阵返回 null

[英]During Xml deserialization, matrix is returning null

Got lots of trials to deserialize the following XML file:进行了大量反序列化以下 XML 文件的试验:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="80" height="50" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1">
 <tileset firstgid="1" name="TilesetSA" tilewidth="16" tileheight="16" tilecount="4000" columns="80">
  <image source="../../TilesetSA.png" width="1280" height="800"/>
 </tileset>
 <layer id="1" name="Walls" width="80" height="50">
  <data encoding="csv">
3,3,3,3,3,3,3,3,3,3,3,3,3,3,
3,81,81,81,81,81,81,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,3,3,3,3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,3,3,3,3,3,3,
</data>
 </layer>
</map>

I don't have the opportunity to change the xml file, it must stay as is.我没有机会更改 xml 文件,它必须保持原样。 I Managed to retreive all attributes for "map", "tileset" and "image", but in the "layer" element, in can only retreive the attributes (id, name, width, height), the data element always remains null.我设法检索“地图”、“图块集”和“图像”的所有属性,但在“图层”元素中,只能检索属性(id、名称、宽度、高度),数据元素始终为空。

I created dedicated classes for "map", "tileset", "layer", "image" and "map".我为“map”、“tileset”、“layer”、“image”和“map”创建了专门的类。

Would anyone suggest me any code to solve this "null result" issue?有人会建议我使用任何代码来解决这个“空结果”问题吗?

Use following xml linq :使用以下 xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication159
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            string dataStr = (string)doc.Descendants("data").FirstOrDefault();

            int[][] results = dataStr.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(y => int.Parse(y)).ToArray()).ToArray();
        }
    }
}

My code is as follow:我的代码如下:

        public virtual void WriteSaveXmlData(string pPath, int pRoom, bool pNewGame, int pX, int pY)
    {
        //xml save serialization
        XmlSaving XmlGameSave = new XmlSaving();
        XmlGameSave.SaveRootPath = pPath;
        XmlGameSave.SavedCurrentRoom = pRoom;
        XmlGameSave.XmlIsNewGame = pNewGame;
        XmlGameSave.XHero = pX;
        XmlGameSave.YHero = pY;

        XmlSerializer xs = new XmlSerializer(typeof(XmlSaving));

        using (StreamWriter wr = new StreamWriter(GameRound.GameSaveRootPath + GameRound.SaveFileName))
        {
            xs.Serialize(wr, XmlGameSave);
        }
    }
    public virtual XmlSaving ReadSaveXmlData()
    {
        //xml save deserialization

        XmlSerializer xs = new XmlSerializer(typeof(XmlSaving));

        using (StreamReader rd = new StreamReader(GameRound.GameSaveRootPath + GameRound.SaveFileName))
        {
            XmlSaving XmlGameSave = xs.Deserialize(rd) as XmlSaving;

            return XmlGameSave;

        }

    }
    public virtual XmlMap ReadXmlMapData(string pPath, int RoomNumber)
    {

        XmlMap ReadTmxData = new XmlMap();
        XmlSerializer TmxXmlMap = new XmlSerializer(typeof(XmlMap));
        using (StreamReader rd = new StreamReader(pPath + @"P1\RoomMapSaphir0" + RoomNumber + ".tmx"))
        {
            ReadTmxData = TmxXmlMap.Deserialize(rd) as XmlMap;
            return ReadTmxData;
        }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM