【问题标题】:read xml root node into datagrid and write edits back to xml file将 xml 根节点读入数据网格并将编辑内容写回 xml 文件
【发布时间】:2012-10-11 04:40:29
【问题描述】:
        this.dataGrid1 = new System.Windows.Forms.DataGrid();
        this.dataGrid1.DataMember = textBox1.Text.ToString();
        this.dataGrid1.Location = new System.Drawing.Point(36, 46);
        this.dataGrid1.Name = "dataGrid1";
        this.dataGrid1.Size = new System.Drawing.Size(364, 532);
        this.dataGrid1.TabIndex = 0;
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(50, 13);
        this.ClientSize = new System.Drawing.Size(592, 573);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.dataGrid1 });
        ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
        this.ResumeLayout(false);


        XmlDataDocument xmlDatadoc = new XmlDataDocument();
        xmlDatadoc.DataSet.ReadXml("abcd.xml");

        DataSet ds = new DataSet("abc");
        ds = xmlDatadoc.DataSet;

        dataGrid1.DataSource = ds.DefaultViewManager; 

它显示了层次结构,但没有正确区分子节点和父节点。我只想查看应该是其子节点链接的根节点。另外,我希望数据网格能够编辑 xml 文件。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    要将任何编辑写回 xml 文件,请使用以下命令:

    DataSet ds= ((DataTable)datagrid1.dataSource).DataSet; 
    //this statement populates the dataset.
    //which is reflected in xml file as-- 
    ds.WriteXml(s, XmlWriteMode.IgnoreSchema); 
    ds.AcceptChanges();
    

    【讨论】:

      【解决方案2】:

      读取DataSet 中的Xml 后,为什么不将DataTable 而不是整个DataSet 设置为Grid 的DataSource?

          XmlDataDocument xmlDatadoc = new XmlDataDocument();
          xmlDatadoc.DataSet.ReadXml("abcd.xml");
      
          DataSet ds = new DataSet("abc");
          ds = xmlDatadoc.DataSet;
      
          dataGrid1.DataSource = ds.Tables[0]; 
      

      使用适当的根表调整表索引。

      【讨论】:

      • 这行得通。现在你能告诉我如何编辑数据网格,以便可以编辑xml中的相应值吗?
      • 这有帮助吗:codeproject.com/Articles/9697/… BTW,你需要在更新后再次从 DataTable 中获取 xml。
      • 这个链接没有帮助。实际上在 pgm 运行时我想编辑数据网格,以便更改的值可以反映在 xml 文件中。
      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多