XML の属性を使用して、ユーザー ログインから名前を付けることができます。
static public void CreateFile(string username)
{
XmlWriter xmlW = XmlWriter.Create(username + ".xml");
xmlW.WriteStartDocument();
xmlW.WriteStartElement("Listofboxs");
//add the box following this canvas
xmlW.WriteStartElement("box");
xmlW.WriteAttributeString("nameofbox", "exampleName");
xmlW.WriteAttributeString("valueofbox", "exampleValue");
xmlW.WriteEndElement();
//
xmlW.WriteEndElement();
xmlW.WriteEndDocument();
xmlW.Close();
}
これにより、ユーザー名で最初のファイルを作成できます。次に、アプリケーションのリロード時にこれらの情報を表示するには、次のコードを使用します。
static public Dictionary<string, string> getBoxValue(string username)
{
Dictionary<string, string> listofbox = new Dictionary<string, string>();
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"./" + username + ".xml");
XmlNode root = xmldoc.DocumentElement;
foreach (XmlNode box in root)
{
listofbox.Add(box.Attributes[0].Value.ToString(),box.Attributes[1].Value.ToString());
}
return listofbox;
}
ノードごとに、辞書はボックスの名前とその値の文字列のペアを追加します。ボックスを埋めるために使用できます。このコードは少し非効率的かもしれませんが (「using」などを使用する必要があります)、お役に立てば幸いです。