1

我在将 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;
        }
    }

我究竟做错了什么???

4

3 回答 3

0

尝试这个:

           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);
        }
于 2011-05-30T09:01:10.380 回答
0

以下 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>

此代码将加载它,并将所有图块提取到可枚举列表中。请记住将 ausing System.Xml.Linq放在文件顶部并针对最近的框架进行构建(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-values 作为整数数组,则以下 LINQ 查询将起作用:

int[] allXValues = (from tile in doc.Descendants("Tile")
                    select (int)tile.Element("X")).ToArray();    
于 2011-05-30T09:01:37.470 回答
0

您的ReadXmlFile方法实际上并没有做任何事情。

考虑使用XmlDocumentorXDocument代替XmlWriterand XmlReader。它们更容易处理。

于 2011-05-30T08:50:37.217 回答