将XML转换为String []帮助

| 我在将XML文档的内容转换为int []或string []时遇到麻烦 我正在将屏幕上20个不同图片框的x和y坐标(用于拼图程序)保存到xml文件,现在正尝试加载已保存的坐标并将拼图碎片更新到那些已保存的位置。 这是我的代码:
    XmlWriter XmlWriter1;
    XmlReader XmlReader1;
    private void Form1_Load(object sender, EventArgs e)
    {
        //-------------------------------------------------
        //Load Events
        //-------------------------------------------------
        SavedPositions = new int[40];
    }
    //-------------------------------------------------------
    //Saves The Current Tile Locations To A Hidden XML File
    //-------------------------------------------------------
    public void SavePicPositionsXML()
    {
        using (XmlWriter1 = XmlWriter.Create(\"SavedPuzzle.xml\"))
        {
            XmlWriter1.WriteStartDocument();
            XmlWriter1.WriteStartElement(\"MTiles\");
            for (int i = 0; i < JigsawImgCount; i++)
            {
                XmlWriter1.WriteStartElement(\"Tile\");
                XmlWriter1.WriteElementString(\"X\",Convert.ToString(MTiles[i].Pic.Location.X));
                XmlWriter1.WriteElementString(\"Y\",Convert.ToString(MTiles[i].Pic.Location.Y));
                XmlWriter1.WriteEndElement();
            }
            XmlWriter1.WriteEndElement();
            XmlWriter1.WriteEndDocument();
        }
    }
    //---------------------------------------------------------------
    //Reads Text From A Hidden Xml File & Adds It To A String Array
    //---------------------------------------------------------------
    private int ReadXmlFile(int Z)
    {
        XmlReader1 = XmlReader.Create(\"SavedPuzzle.xml\");
        XmlReader1.MoveToContent();
        while (XmlReader1.Read())
        {

        }
        //    SavedPositions[B] = Convert.ToInt32(XmlReader1.Value.ToString());
        return SavedPositions[Z];
    }
    //-------------------------------------------------
    //Loads Saved Tile Positions From A Hidden Xml File
    //------------------------------------------------- 
    private void LoadPositionsXML()
    {
        G = 0;
        for (int i = 0; i < JigsawImgCount; i++)
        {
            LineX = ReadXmlFile(G);
            LineY = ReadXmlFile(G + 1);
            MTiles[i].Pic.Location = new Point(LineX, LineY);
            G = G + 2;
        }
    }
我究竟做错了什么???     
已邀请:
        您的
ReadXmlFile
方法实际上并没有做任何事情。 考虑使用
XmlDocument
XDocument
代替
XmlWriter
XmlReader
。它们更容易处理。     
        尝试这个:
           XmlDocument document = new XmlDocument();
        document.Load(@\"D:/SavedPuzzle.xml\");
        XmlNode topNode = document.GetElementsByTagName(\"MTiles\")[0];
        foreach (XmlNode node in topNode.ChildNodes)
        {
            int X = Int32.Parse(node.ChildNodes[0].InnerText);
            int Y = Int32.Parse(node.ChildNodes[1].InnerText);
        }
    
        下面的LinqToXML语句将所有切片按存储在文档中的顺序提取到列表中。 我假设一个XML文件如下所示:
<xml>
  <MTiles>
    <Tile>
        <X>1</X>
        <Y>10</Y>
    </Tile>
    <Tile>
        <X>2</X>
        <Y>20</Y>
    </Tile>
    <Tile>
        <X>3</X>
        <Y>30</Y>
    </Tile>
    <Tile>
        <X>4</X>
        <Y>40</Y>
    </Tile>
  </MTiles>
</xml>
此代码将加载它,并将所有图块提取到一个可枚举的列表中。切记在文件的顶部放置一个ѭ8,并根据最新的框架构建(IIRC,它是.NET 3.5中引入的)
XDocument doc = XDocument.Load(/* path to the file, or use an existing reader */);
var tiles = from tile in doc.Descendants(\"Tile\")
            select new
            {
                X = (int)tile.Element(\"X\"),
                Y = (int)tile.Element(\"Y\"),
            };
foreach (var tile in tiles)
{
    Console.WriteLine(\"Tile: x={0}, y={1}\", tile.X, tile.Y);
}
上面的代码使用我指定的XML文件的输出为:
Tile: x=1, y=10
Tile: x=2, y=20
Tile: x=3, y=30
Tile: x=4, y=40
编辑: 如果只想将所有
X
值作为整数数组,则以下LINQ查询将起作用:
int[] allXValues = (from tile in doc.Descendants(\"Tile\")
                    select (int)tile.Element(\"X\")).ToArray();    
    

要回复问题请先登录注册