将xml文件加载到Datagrid视图并在更新后再次保存

时间:2016-07-11 10:01:36

标签: c# xml datagridview

我有一个像这样的xml文件:

<xml version="1.0" encoding="UTF-8">

<configes>

<Username>Administrator</Username>

<Password>123456</Password>

<Domain>sample</Domain>

<Url>http://sample/Organization.svc</Url>

</configes>

我把它加载到datagridview中是这样的:

DataGridViewRow row;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(txtXmlAdd.Text);
XmlElement elm = xmlDoc.DocumentElement;
XmlNodeList nodeList =  elm.ChildNodes;
foreach (XmlNode node in nodeList)
{
    row = (DataGridViewRow)dgvXML.Rows[0].Clone();
    row.Cells[0].Value = node.Name ;
    row.Cells[1].Value = node.InnerText;
    dgvXML.Rows.Add(row);
 }

现在,我想更改一些属性(编辑值或添加新属性)并再次将其保存到同一文件中。 我试试这个,但它不起作用:

DataTable dT = GetDataTableFromDGV(dgvXML);
DataSet dS = new DataSet();
dS.DataSetName = "configes";
dS.Tables.Add(dT);
dS.WriteXml(string path);

当GetDataTableFromDGV是这样的时候:

var dt = new DataTable();
dt.TableName="";            
string firstCol = "";
string secCol = "";
object[] cellValues = new object[1];
foreach (DataGridViewRow row in dgv.Rows)
{
    secCol = row.Cells[1].Value.ToString();
    firstCol = row.Cells[0].Value.ToString();
    dt.Columns.Add(firstCol);
    cellValues[0] = row.Cells[1].Value;                    
    dt.Rows.Add(cellValues);
}

return dt;

2 个答案:

答案 0 :(得分:2)

做到这一点。

定义表单字段(不是局部变量):

DataSet dataSet = new DataSet();

从文件加载数据:

dataSet.ReadXml(filename);

将DataTable绑定到DataGridView:

dataGridView.DataSource = dataSet.Tables[0];

数据将显示在DataGridView中。现在你可以编辑它们了。

将数据保存到文件中:

dataSet.Tables[0].WriteXml(filename);

如果您希望连续显示每列及其值,那么您有两种变体。

  1. 更改xml文件格式,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Root>
      <configes>
        <Name>UserName</Name>
        <Value>Administrator</Value>
      </configes>
      <configes>
        <Name>Password</Name>
        <Value>123456</Value>
      </configes>
      <configes>
        <Name>Domain</Name>
        <Value>sample</Value>
      </configes>
      <configes>
        <Name>Url</Name>
        <Value>http://sample/Organization.svc</Value>
      </configes>
    </Root>
    

    在这种情况下,您可以使用上面简短而干净的C#代码。

  2. 如果您无法更改xml文件的格式,则必须手动解析它并将值放入DataTable中。

答案 1 :(得分:0)

正如我在评论中告诉你的那样,问题的解决方案在这里是一个旧答案:Editing xml on live in C#. Deleting nodes that contain specific value

但如果你愿意,我可以为你改写....

假设您将xml文件存储在dataTable中,如果您编辑此dataTable中的数据,则可以像第一个一样重写您的xml文件。

//READ THE XML FILE
XmlDocument xmlDoc = new XmlDocument();
//My path
xmlDoc.LoadXml(Properties.Resources.test);

//Read the xml file into a dataSet
DataSet ds = new DataSet();
XmlNodeReader xnr = new XmlNodeReader(xmlDoc);
ds.ReadXml(xnr);

//Your data will be store in the 4's dataTable of the dataSet ( the <field> )
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
    //RemoteAt will remove all the node, so the node <Field> in your example data
    ds.Tables[0].Rows[0][i] = "NEWVALUE";
    //If you want to only remove the node <Value>  (and not all the <Field> node ) just do ds.Tables[4].Rows["Value"]=null;
}
//Write your new content in a new xml file

//As you wanted here you just read the new xml file created as a string
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    ds.WriteXml(xmlTextWriter);
    xmlTextWriter.Flush();
    stringWriter.GetStringBuilder().ToString();
    //Here the result is in stringWriter,  and  there is 6 <Field> nodes, and not 8 like before the suppress
}

//If you want to create a new xml file with the new content just do 
ds.WriteXml(pathOfTheFirstXmlFile);
//( like rewriting the previous xml file )

我的输入:(test.xml)

<?xml version="1.0" encoding="UTF-8"?>

<configes>
<Username>Administrator</Username>
<Password>123456</Password>
<Domain>sample</Domain>
<Url>http://sample/Organization.svc</Url>
</configes>

上一段代码之后的输出:

<?xml version="1.0" standalone="yes"?>
<configes>
  <Username>NEWVALUE</Username>
  <Password>NEWVALUE</Password>
  <Domain>NEWVALUE</Domain>
  <Url>NEWVALUE</Url>
</configes>