0

I have created the file but it cannot get loaded back to the gridview when i tried to browse it and upload, it shows the error of "Cannot find table 2". i don't know whats the problem because now the file that is being created it's no longer the same as the original file. original file table name its called data and after creating the file the table name became Table1

this is how i write to the file. mybe any suggestion on easiest way to write to the file will help.

    private DataTable GetDataTableFromDataGridview(DataGridView _grid)
    {
      {
            var _oDataTable = new DataTable();
            object[] cellValues = new object[_grid.Columns.Count];
           _oDataTable.Columns.Add("Name", typeof(string));
            _oDataTable.Columns.Add("Value", typeof(string));
            _oDataTable.Columns.Add("Font", typeof(string));
            _oDataTable.Columns.Add("DateStamp", typeof(DateTime));
            _oDataTable.Columns.Add("Comment", typeof(string));
            foreach (DataGridViewRow row in _grid.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    cellValues[i] = row.Cells[i].Value;   
                }
                _oDataTable.Rows.Add(cellValues.ToArray());
            }
            return _oDataTable;
        }         
    }

Save button

    private void btnSave_Click(object sender, EventArgs e)
    {
            string outputFilePath = txtInputfile.Text.Replace(_InputFileName, _OutFileName);
            XDocument doc = XDocument.Load(outputFilePath);

            DataTable dataTable = GetDataTableFromDataGridview(Gridview_Output);
            DataSet dataSet = new DataSet();
            dataSet.Tables.Add(dataTable);
            dataSet.WriteXml(outputFilePath);
            MessageBox.Show("New file created,testing ");
    }

original file

<data name="Label" xml:space="preserve">
<value></value>
<comment>[Font][/Font][DateStamp][/DateStamp][Comment][/Comment]</comment>
</data>
<data name="Exit_Button" xml:space="preserve">
<value></value>
<comment>[Font][/Font][DateStamp][/DateStamp][Comment][/Comment]</comment>
</data>
<data name="Exit_Verify_Message" xml:space="preserve">
<value></value>
<comment>[Font][/Font][DateStamp][/DateStamp][Comment][/Comment]</comment>

results after creating new file

  <?xml version="1.0" standalone="yes"?>
  <NewDataSet>
  <Table1>
  <Name>FinEnrolment_CurrentSelectedUser_Label</Name>
  <Value>dfsd</Value>
  <Font>fdsf</Font>
  <DateStamp>2015-02-03T10:56:50+02:00</DateStamp>
  <Comment>dfd</Comment>
  </Table1>
  <Table1 />
</NewDataSe>
IT Forward
  • 367
  • 2
  • 7
  • 28

1 Answers1

2

The reason you get this result is because the contents of the comment tag is not xml.

so you will have to write a converter from your DataTable to the resx file structure you had before.

All this would be handled with the classes i provided you allready.

How do i get xml nodes from the xmlnodelist

I still can't figure out why you have to do this in this (way more complicated) manner.

If you really have to write the conversion it would look like this:

private static void CopyValuesFromDataTableToXml(string fileName, DataTable table)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(fileName);
        foreach (DataRow row in table.Rows)
        {
            string name = (string)row["Name"];
            //fish out the element out of the xml
            XmlElement element = doc.SelectSingleNode(string.Format("//data[@name='{0}']", name)) as XmlElement;
            //set value
            element.SelectSingleNode("./value").InnerText = (string)row["Value"];
            //set comment
            element.SelectSingleNode("./comment").InnerText = 
                string.Format(
                "[Font]{0}[/Font][DateStamp]{1}[/DateStamp][Comment]{2}[/Comment]",
                row["Font"],
                row["DateStamp"],
                row["Comment"]);
        }

        //here would belong the code to update the version

        doc.Save(fileName);
    }
Community
  • 1
  • 1
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28