将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;
}
}
我究竟做错了什么???
没有找到相关结果
已邀请:
3 个回复
悍蕾驮苇袜
方法实际上并没有做任何事情。 考虑使用
或
代替
和
。它们更容易处理。
拟僚疽刊剔
镀建啼
此代码将加载它,并将所有图块提取到一个可枚举的列表中。切记在文件的顶部放置一个ѭ8,并根据最新的框架构建(IIRC,它是.NET 3.5中引入的)
上面的代码使用我指定的XML文件的输出为:
编辑: 如果只想将所有
值作为整数数组,则以下LINQ查询将起作用: