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("Config Opt");
DataTable dt = new DataTable("sett ings");
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(d t);
DataTable dt1 = new DataTable("sett ings_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(ro w);
}
ds.Tables.Add(d t1);
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.Seri alization;
[System.Xml.Seri alization.XmlRo otAttribute(Nam espace="", IsNullable=fals e)]
public class ConfigOpt {
[System.Xml.Seri alization.XmlEl ementAttribute( "settings",
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public ConfigOptSettin gs[] Items;
}
public class ConfigOptSettin gs {
[System.Xml.Seri alization.XmlEl ementAttribute( "record",
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied)]
public ConfigOptSettin gsRecord[] record;
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public string id;
}
public class ConfigOptSettin gsRecord {
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
public string Field_1;
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
public string Field_2;
}
=============== =============== ===============
Then, I can create the xml config file via the following code:
=============== =============== ======
private void btnSerialize_Cl ick(object sender, System.EventArg s e)
{
try
{
ConfigOpt config = new ConfigOpt();
config.Items = new ConfigOptSettin gs[5];
for(int i=1;i<=config.I tems.Length;i++ )
{
ConfigOptSettin gs setting = new ConfigOptSettin gs();
setting.id = "s" + i;
setting.record = new ConfigOptSettin gsRecord[3];
for(int j=1;j<=setting. record.Length;j ++)
{
ConfigOptSettin gsRecord record = new ConfigOptSettin gsRecord();
record.Field_1 = "Field1_Val ue";
record.Field_2 = "Field2_Val ue";
setting.record[j-1] = record;
}
config.Items[i-1] = setting;
}
XmlSerializer x = new XmlSerializer(t ypeof(ConfigOpt ));
TextWriter writer = new StreamWriter("o utput.xml");
x.Serialize(wri ter, 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?f rame=true
#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri
alizationwithxm lserializer.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