all groups > dotnet xml > august 2003 >
You're in the

dotnet xml

group:

Mapping a datatable column to an xml element



Mapping a datatable column to an xml element Anita C
8/12/2003 4:24:05 PM
dotnet xml: Hi,
How do I associate or map a specific column in a datatable to a particular
element present in an xml document - to read into a datatable as well as
write from the datatable to the xml element?
Also, how can I associate all the attributes and their values of a
particular element to the Name & Value columns of a datatable - to read
into a datatable as well as write from the datatable to the xml element?

Any help will be greatly appreciated.

Thanks.

This sample code I did might help you Mike in Paradise NL
8/14/2003 8:54:48 AM
This code takes an XML file with a schema and lets you
select the XML file and then shows the Tables with a Tab
page for the Data and one for the Table layout. This will
give you an idea.

I have the Book ADO.Ndet by Davia Sceppa (microsoft press)
and it has helped me a lot in this.

There is also a routine in here to automatically set the
column width that was copied from the codeguru site
referenced in the comments in the program.

Have fun!



//====================================================
//XMLDataGrid by Mike in Paradise,NL
//
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Project1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private string dataBaseName;
private string dataBasePath;
private DataSet theDataSet;
private DataTable columnsTable;
private DataTable selectedTable;
private ArrayList tableList = new ArrayList();

private System.Windows.Forms.ComboBox tableBox;
private System.Windows.Forms.Button changeDataSourceButton;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.DataGrid dataBaseDataGrid;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button saveButton;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after
InitializeComponent call
//
dataBasePath="../../Data/";
dataBaseName=dataBasePath+"Data.xml";
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableBox = new System.Windows.Forms.ComboBox();
this.changeDataSourceButton = new
System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.dataBaseDataGrid = new System.Windows.Forms.DataGrid
();
this.saveButton = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)
(this.dataGrid1)).BeginInit();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)
(this.dataBaseDataGrid)).BeginInit();
this.SuspendLayout();
//
// tableBox
//
this.tableBox.ItemHeight = 13;
this.tableBox.Location = new System.Drawing.Point(176, 16);
this.tableBox.Name = "tableBox";
this.tableBox.Size = new System.Drawing.Size(192, 21);
this.tableBox.TabIndex = 12;
this.tableBox.SelectedIndexChanged += new
System.EventHandler(this.tableBox_SelectedIndexChanged);
//
// changeDataSourceButton
//
this.changeDataSourceButton.Location = new
System.Drawing.Point(24, 16);
this.changeDataSourceButton.Name
= "changeDataSourceButton";
this.changeDataSourceButton.Size = new System.Drawing.Size
(128, 23);
this.changeDataSourceButton.TabIndex = 14;
this.changeDataSourceButton.Text = "Load Data Source";
this.changeDataSourceButton.Click += new
System.EventHandler(this.changeDataSourceButton_Click);
//
// panel1
//
this.panel1.Controls.Add(this.saveButton);
this.panel1.Controls.Add(this.tableBox);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(736, 48);
this.panel1.TabIndex = 15;
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock =
System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0,
48);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(736, 165);
this.tabControl1.TabIndex = 17;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.dataGrid1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Size = new System.Drawing.Size(728, 139);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "Data";
//
// dataGrid1
//
this.dataGrid1.CaptionVisible = false;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(728, 139);
this.dataGrid1.TabIndex = 1;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.dataBaseDataGrid);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Size = new System.Drawing.Size(728, 131);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "Schema";
//
// dataBaseDataGrid
//
this.dataBaseDataGrid.DataMember = "";
this.dataBaseDataGrid.Dock =
System.Windows.Forms.DockStyle.Fill;
this.dataBaseDataGrid.HeaderForeColor =
System.Drawing.SystemColors.ControlText;
this.dataBaseDataGrid.Location = new System.Drawing.Point
(0, 0);
this.dataBaseDataGrid.Name = "dataBaseDataGrid";
this.dataBaseDataGrid.Size = new System.Drawing.Size(728,
131);
this.dataBaseDataGrid.TabIndex = 17;
//
// saveButton
//
this.saveButton.Location = new System.Drawing.Point(408,
16);
this.saveButton.Name = "saveButton";
this.saveButton.Size = new System.Drawing.Size(144, 23);
this.saveButton.TabIndex = 13;
this.saveButton.Text = "Save Changes";
this.saveButton.Click += new System.EventHandler
(this.saveButton_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(736, 213);
this.Controls.Add(this.changeDataSourceButton);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)
(this.dataGrid1)).EndInit();
this.tabPage2.ResumeLayout(false);
Re: This sample code I did might help you Anita C
8/14/2003 2:23:08 PM
Thanks.

[quoted text, click to view]
AddThis Social Bookmark Button