469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How-To Dynamically Create Nested DataTables

I can create this:

?xml version="1.0" standalone="yes" ?>
<ConfigOpt>
<record>
<Field_1>Text # 1</Field_1>
<Field_2>Text # 2</Field_2>
</record>
</ConfigOpt>

I want to create this:

<?xml version="1.0" standalone="yes" ?>
<ConfigOpt>
<settings>
<record>
<Field_1>Text # 1</Field_1>
<Field_2>Text # 2</Field_2>
</record>
<record>
<Field_1>Text # 3</Field_1>
<Field_2>Text # 4</Field_2>
</record>
<record>
<Field_1>Text # 5</Field_1>
<Field_2>Text # 6</Field_2>
</record>
</settings>
</ConfigOpt>

How do I nest the table. I am eventually going to add in a new <settings_2>
section that will have its own <record> collection.

<?xml version="1.0" standalone="yes" ?>
<ConfigOpt>
<settings>
<record>
<Field_1>Text # 1</Field_1>
<Field_2>Text # 2</Field_2>
</record>
<record>
<Field_1>Text # 3</Field_1>
<Field_2>Text # 4</Field_2>
</record>
</settings>
<settings_2>
<record>
<Field_1>Text # 5</Field_1>
<Field_2>Text # 6</Field_2>
</record>
<record>
<Field_1>Text # 7</Field_1>
<Field_2>Text # 8</Field_2>
</record>
</settings_2>
</ConfigOpt>
This **** MUST **** be done at run time. I need to create a new
<setting_#> section as well as the <record> and add the fields in.

Any help would be appreciated.

Thanks,
Dave
Nov 12 '05 #1
2 8594
Hi David,

From your description, You have a certain xml configuration file which
store some certain records information. The records are organized by
different tables(settings element), so you're wondering how dynamically add
arbitrary number of such tables, yes?

Based on my research, I think we should use the Xml Serialization feature
of the .net framework. In fact, I first try using a DataSet and then
creating several DataTables and add into the DataSet , but I found the
output xml is not what we want. For example, the following code
===========================
DataSet ds = new DataSet("ConfigOpt");
DataTable dt = new DataTable("settings");
dt.Columns.Add("Field_1");
dt.Columns.Add("Field_2");

DataRow row = null;
for(int i=1;i<=2;i++)
{
row = dt.NewRow();
row[0] = "Value_1_" + i;
row[1] = "Value_2_" + i;

dt.Rows.Add(row);
}

ds.Tables.Add(dt);

DataTable dt1 = new DataTable("settings_2");
dt1.Columns.Add("Field_1");
dt1.Columns.Add("Field_2");
for(int i=1;i<=2;i++)
{
row = dt1.NewRow();
row[0] = "Value_1_" + i;
row[1] = "Value_2_" + i;

dt1.Rows.Add(row);
}

ds.Tables.Add(dt1);

MessageBox.Show(ds.GetXml());
==============================
will output the following xml:

===================================
<ConfigOpt>

<settings>

<Field_1>Value_1_1</Field_1>

<Field_2>Value_2_1</Field_2>

</settings>

<settings>

<Field_1>Value_1_2</Field_1>

<Field_2>Value_2_2</Field_2>

</settings>

<settings_2>

<Field_1>Value_1_1</Field_1>

<Field_2>Value_2_1</Field_2>

</settings_2>

<settings_2>

<Field_1>Value_1_2</Field_1>

<Field_2>Value_2_2</Field_2>

</settings_2>

</ConfigOpt>
=============================

Now, we turn to the Xml Serialization means, first we need to create the
serialization class for the config file you want to manage. Fortunately,
the dotnet framework provide a tool named "xsd.exe" which can help generate
the xml serialization class from a xml file( what we want the class be
serialized to)

#XML Schema Definition Tool (Xsd.exe)
http://msdn.microsoft.com/library/en...chemaDefinitio
nToolXsdexe.asp?frame=true

Then, I use the xsd.exe create the following classes for serializing:
===============================
using System.Xml.Serialization;

[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=false)]
public class ConfigOpt {

[System.Xml.Serialization.XmlElementAttribute("sett ings",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ConfigOptSettings[] Items;
}

public class ConfigOptSettings {

[System.Xml.Serialization.XmlElementAttribute("reco rd",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ConfigOptSettingsRecord[] record;

[System.Xml.Serialization.XmlAttributeAttribute()]
public string id;
}

public class ConfigOptSettingsRecord {
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string Field_1;
[System.Xml.Serialization.XmlElementAttribute(Form= System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string Field_2;
}
=============================================

Then, I can create the xml config file via the following code:

====================================
private void btnSerialize_Click(object sender, System.EventArgs e)
{
try
{
ConfigOpt config = new ConfigOpt();

config.Items = new ConfigOptSettings[5];

for(int i=1;i<=config.Items.Length;i++)
{
ConfigOptSettings setting = new ConfigOptSettings();
setting.id = "s" + i;
setting.record = new ConfigOptSettingsRecord[3];

for(int j=1;j<=setting.record.Length;j++)
{
ConfigOptSettingsRecord record = new ConfigOptSettingsRecord();
record.Field_1 = "Field1_Value";
record.Field_2 = "Field2_Value";

setting.record[j-1] = record;
}

config.Items[i-1] = setting;
}

XmlSerializer x = new XmlSerializer(typeof(ConfigOpt));
TextWriter writer = new StreamWriter("output.xml");
x.Serialize(writer, config);
writer.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
==============================================

The output file(output.xml) is like below:
==============================
<?xml version="1.0" encoding="utf-8"?>
<ConfigOpt xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<settings id="s1">
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
</settings>
...........................
..................................
<settings id="s5">
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
<record>
<Field_1>Field1_Value</Field_1>
<Field_2>Field2_Value</Field_2>
</record>
</settings>
</ConfigOpt>
======================================

#Note that since the xml serializing requre each class or member has a
fixed "element name", so we can use the names such as
<settings> ... <settings_1> ..<settings_2>

I use a "id" attribute to identify them, <settings id="...">

In addition, here are some other reference on .net 's xml serialization:
#Introducing XML Serialization
http://msdn.microsoft.com/library/en...oducingxmlseri
alization.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/de...us/cpguide/htm

Hope also helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
Nov 12 '05 #2
Thanks Dave,

I'm glad that my suggestions are of assistance.
Good luck!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tom Szabo | last post: by
15 posts views Thread by Reid Nichol | last post: by
2 posts views Thread by Frances Del Rio | last post: by
4 posts views Thread by Supra | last post: by
reply views Thread by candra | last post: by
2 posts views Thread by belred | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.