将XML转换为String []帮助

时间:2011-05-30 08:44:33

标签: c# xml arrays writer

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

我做错了什么???

3 个答案:

答案 0 :(得分:0)

您的ReadXmlFile方法没有做任何事情。

考虑使用XmlDocumentXDocument代替XmlWriterXmlReader。它们更容易处理。

答案 1 :(得分: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);
        }

答案 2 :(得分: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>

此代码将加载它,并将所有切片提取到可枚举列表中。记得在文件的顶部放置一个using 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 - 值作为整数数组,则以下LINQ查询将起作用:

int[] allXValues = (from tile in doc.Descendants("Tile")
                    select (int)tile.Element("X")).ToArray();