读取XML根节点到DataGrid和编辑写回XML文件

读取XML根节点到DataGrid和编辑写回XML文件

本文介绍了读取XML根节点到DataGrid和编辑写回XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        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;



这是显示了分层结构,但它不是子和父节点区分properly.i只想看看这应该是其子node.also链接根节点,我想数据网格要能够编辑XML文件。

It is showing the hierarchial structure but it is not distinguishing between the child and parent node properly.i only want to see the root node which should be a link to its child node.also,i want the datagrid to be able to edit the xml file.

推荐答案

阅读DataSet中的Xml你为什么不设置数据表,而不是整个数据集作为网格的数据源后?

After reading the Xml in DataSet why dont you set the DataTable rather than the whole DataSet as DataSource of the Grid ?

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

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

    dataGrid1.DataSource = ds.Tables[0];

调整与相应的根表的表索引。

Adjust the Table index with your appropriate Root table.

这篇关于读取XML根节点到DataGrid和编辑写回XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:12