2015-03-02 51 views
0

的XML文件,但它不能让装回当我试图浏览并上传GridView控件,它显示的错误“无法找到表2”。我不知道最新的问题,因为现在正在创建的文件不再与原始文件相同。原始文件表名其称为数据和创建文件后,该表的名字成为表1如何重装创建成我所创建的文件在DataGridView

这是我写的文件。对于写入文件最简单的方法,任何建议都会有所帮助。

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; 
     }   
    } 

保存按钮

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 "); 
    } 

原始文件

<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> 

结果创建新文件之后

<?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> 

回答

2

你得到这个结果的原因是因为注释的内容标记不是xml。

,所以你必须从你的数据表中的转换器给你写信之前所拥有的RESX文件结构。

所有这一切都将与我提供你准备好的课程一起处理。

How do i get xml nodes from the xmlnodelist

我仍然无法弄清楚,为什么你要做到这一点在这种(方式更复杂)的方式。

如果你真的有写的转换就应该是这样的:

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); 
    }