1

フログラムに新しいチェックボックスを追加し、その値を設定ファイルに保存する必要があります。私は次のコードでそれをやろうとしています:

private void CloseDisconnectedCbx_CheckedChanged(object sender, EventArgs e)
{
    XDocument doc = XDocument.Load(BotsFile);
    var savedBots = doc.Descendants("SavedBots")
        .Where(p => p.Element("BotName").Value.ToLower()
                    == SelectBotBox.SelectedItem.ToString().ToLower())
        .Elements("CloseDisconnected").FirstOrDefault();
    if (savedBots == null)
    {
        try
        {
            doc.Descendants("SavedBots")
               .Where(p => p.Element("BotName").Value.ToLower()
                           == SelectBotBox.SelectedItem.ToString().ToLower())
               .FirstOrDefault()
               .Add(new XElement("CloseDisconnected",
                    Convert.ToInt32(CloseDisconnectedCbx.Checked)));
            doc.Save(BotsFile);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

新しい要素が追加されますが、次のようになります。

<CloseDisconnected/> VALUE

要素の終了を終了することはありません。コードが間違っているのでしょうか、それとも何かを忘れてしまったのでしょうか。

このコードは、要素がXMLファイルに見つからない場合にのみトリガーされることになっています。そうである場合、変更は別のボタンで処理されます。

4

1 に答える 1

0
Check parenthesis in your code

XElement xElem = new XElement("root");
xElem.Add( new XElement("CloseDisconnected", "123") ); // generates what you expect
xElem.Add( new XElement("CloseDisconnected"), "123"); // generates what you see
于 2012-07-30T14:12:31.513 に答える