Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除添加的行并将其保存为XML_C#_Xml_Winforms_Datagridview - Fatal编程技术网

C# 删除添加的行并将其保存为XML

C# 删除添加的行并将其保存为XML,c#,xml,winforms,datagridview,C#,Xml,Winforms,Datagridview,我已经用C#创建了一个Windows窗体应用程序。使用这个应用程序,我将一个XML文件加载到Datagridview中。此应用程序能够编辑、删除XML文件中的行并将其添加到XML文件中 我创建了第二个表单,可以在其中向XML文件中添加键和值: private void button1_Click(object sender, EventArgs e) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(@"c:\users\khaa

我已经用C#创建了一个Windows窗体应用程序。使用这个应用程序,我将一个XML文件加载到Datagridview中。此应用程序能够编辑、删除XML文件中的行并将其添加到XML文件中

我创建了第二个表单,可以在其中向XML文件中添加键和值:

private void button1_Click(object sender, EventArgs e)
{
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");
    XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
    XmlAttribute xKey = xDoc.CreateAttribute("key");
    XmlAttribute xValue = xDoc.CreateAttribute("value");
    xKey.Value = KeyTextBox.Text;
    xValue.Value = ValueTextBox.Text;
    xNode.Attributes.Append(xKey);
    xNode.Attributes.Append(xValue);
    xDoc.GetElementsByTagName("appSettings")[0].InsertAfter(xNode,
    xDoc.GetElementsByTagName("appSettings")[0].LastChild);    
    xDoc.Save(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");

    this.Close();
}
在我添加这两行之后,它将进入我的datagridview,但是在我想要删除它并尝试保存它之后,这一行不会从XML文件中删除

保存功能:

private void SaveChanges()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");
    XmlNodeList nodes = doc.DocumentElement.SelectNodes("/configuration/appSettings/add");
    //doc.RemoveAll();
    foreach (var item in _settings)
    {

        for (int i = 0; i < nodes.Count; i++)
        {
            if (nodes[i].Attributes[0].Value.Equals(item.Key))
            {
                nodes[i].Attributes[1].Value = item.Value;
            }
        }
    }
}
    private void deleteRowToolStripMenuItem_Click(object sender, EventArgs e)
{

    int rowIndex = dataGridView1.CurrentCell.RowIndex;
    dataGridView1.ClearSelection();
    dataGridView1.DataSource = null;
    _settings.RemoveAt(rowIndex);


    //dataGridView1.DataSource = _settings;
    BindDataGrid();
}
XML文件:

 <?xml version="1.0"?>
 <configuration>
 <appSettings>

 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />
 <add key="" value="" />

</appSettings>
</configuration>

我做错了什么?解决方案是什么?

在“保存更改”方法中,您只更新现有密钥,因此现有密钥将保留,而不会添加新密钥。在保存过程中,需要使用表中的数据完全重写XML文件。此外,在使用“doc.save(…)”进行更改后,还需要保存“XmlDocument”

UPD: 将“SaveChanges”方法更改为:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");
var list = xDoc.DocumentElement["appSettings"];
list.RemoveAll();
foreach (var item in _settings)
{
  XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
  XmlAttribute xKey = xDoc.CreateAttribute("key");
  XmlAttribute xValue = xDoc.CreateAttribute("value");
  xKey.Value = item.Key;
  xValue.Value = item.Value;
  xNode.Attributes.Append(xKey);
  xNode.Attributes.Append(xValue);
  xDoc.GetElementsByTagName("appSettings")[0].InsertAfter(xNode, xDoc.GetElementsByTagName("appSettings")[0].LastChild);
}
xDoc.Save(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");
在“SaveChanges”方法中,您只更新现有密钥,因此现有密钥将保留,而不会添加新密钥。在保存过程中,需要使用表中的数据完全重写XML文件。此外,在使用“doc.save(…)”进行更改后,还需要保存“XmlDocument”

UPD: 将“SaveChanges”方法更改为:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");
var list = xDoc.DocumentElement["appSettings"];
list.RemoveAll();
foreach (var item in _settings)
{
  XmlNode xNode = xDoc.CreateNode(XmlNodeType.Element, "add", "");
  XmlAttribute xKey = xDoc.CreateAttribute("key");
  XmlAttribute xValue = xDoc.CreateAttribute("value");
  xKey.Value = item.Key;
  xValue.Value = item.Value;
  xNode.Attributes.Append(xKey);
  xNode.Attributes.Append(xValue);
  xDoc.GetElementsByTagName("appSettings")[0].InsertAfter(xNode, xDoc.GetElementsByTagName("appSettings")[0].LastChild);
}
xDoc.Save(@"c:\users\khaab\documents\visual studio 2015\Projects\ReadingXML\test.xml");

谢谢你的回复。我不知道该怎么做,你能帮我举个例子或解决方案吗?原因是我是C#Tnx的初学者。我在回复中添加了“SaveChanges”方法的正确代码示例。为了更方便地使用Xml文件,请尝试。谢谢您的回复。我不知道该怎么做,你能帮我举个例子或解决方案吗?原因是我是C#Tnx的初学者。我在回复中添加了“SaveChanges”方法的正确代码示例。为了更方便地使用Xml文件,可以尝试。