Dobrý den, v testovacím projektu mám následující kód (není odladěný pro cestu v xml, ale tím to nebude). Vytvářím synchnně objekt a volá se konstruktor s parametrem cesty k souboru, ale u xmlDoc.Save(fileName) to hlásí chybu IOException, co je v titulku, ale mně je to nepochopitelné. Nevíte, prosím, čím by to mohlo být způsobené? Při synchronním volání? Děkuji předem. H.
Kód je následující:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
namespace TestXml
{
public class HighScoreData
{
private string fileName;
private XmlDocument xmlDoc;
private string pathInXml = "ArkanoidHighScore";
private HighScoreData()
{
}
public HighScoreData(string fileName)
{
this.fileName = fileName;
xmlDoc = new XmlDocument();
if (!File.Exists(fileName))
{
File.Create(fileName);
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
XmlElement root = xmlDoc.DocumentElement;
xmlDoc.InsertBefore(xmlDeclaration, root);
XmlElement childElement = xmlDoc.CreateElement(string.Empty, "ArkanoidHighScore", string.Empty);
xmlDoc.AppendChild(childElement);
xmlDoc.Save(fileName);
}
}
public int LoadHighScore()
{
int highScore = 0;
xmlDoc.Load(fileName);
XmlNode node = xmlDoc.SelectSingleNode(pathInXml);
highScore = int.Parse(node.InnerText);
return highScore;
}
public void SaveHighScore(int score)
{
xmlDoc.Load(fileName);
XmlNode xmlNode = xmlDoc.SelectSingleNode(pathInXml);
if (xmlNode != null)
{
xmlNode.InnerText = score.ToString();
}
xmlDoc.Save(fileName);
}
}
}