Windowsの電話(7.1)でXMLファイルにユーザーデータを保存しようとしています。このプロセスでは、既存のXMLファイルを開き、新しいノードを挿入してXMLファイルを保存しようとします。 Visual Studio 2010ではコードがエラーなく実行されますが、変更はファイルには反映されません。問題は、コードがファイルを別の場所に保存していることが原因と思われます。 XMLWriterをXDocumentのcreateコマンドで入力パラメータとしてファイルへの塗りつぶしパスで作成しようとしましたが、Windows PhoneシステムのSystem.XML.Linq(2.0.5)のサポートされているバージョンでは許可されていません。次のようにWindows Phone 7.1アプリケーションでXMLファイルを編集する方法
コードは次のとおりです。
public void AddSwimGoal(SwimGoal SG)
{
string FileName = "Data/SwimGoals.xml";
XDocument Doc = new XDocument();
Doc = XDocument.Load(@FileName);
XElement Root = Doc.Root;
XElement NewSG = new XElement("SwimGoal");
XAttribute Dist = new XAttribute("Distance", SG.Distance);
XAttribute MaxTD = new XAttribute("MaxTrainingDistance", SG.MaxTrainingDistance);
XAttribute ID = new XAttribute("ID", SG.ID);
XAttribute Name = new XAttribute("Name", SG.Name);
XAttribute StartDate = new XAttribute("StartDate", SG.StartDate);
XAttribute EndDate = new XAttribute("EndDate", SG.EndDate);
XAttribute DesiredTime = new XAttribute("DesiredTime", SG.Desiredtime);
XAttribute Stroke = new XAttribute("Stroke", SG.Stroke);
NewSG.Add(Dist, MaxTD, ID, Name, StartDate, EndDate, DesiredTime, Stroke);
Root.Add(NewSG);
XmlWriter Wr = Doc.CreateWriter();
Doc.Save(Wr);
}
保存したXML文書を再読み込みしようとしましたか? – webdad3
はい、文書は変更されません。 – user640142