473,722 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9094
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2982
by: Tom Szabo | last post by:
Just wandering how can you close a browser remotely. In some web applications the browser closes or refreshes periodically. How is that done and how can it be done through PHP? TIA, Tom
15
16331
by: Reid Nichol | last post by:
Hello, I was wondering if I could control how many bytes are in an int and the byte order. In C/C++ I can use int32 but how do I do this in python? How can I control byte order?
0
1823
by: v I n O | last post by:
hI Geeks Please do let me know how do i find how many instances sql server running on the single machine. or in the n/w my objective should with help of C#/vb.net
4
2027
by: James Salisbury | last post by:
Hi, I was checking my parent's website salisbury.cabrera.net on google by entering villa rent spain cabrera I can see the required site, ranked at 4, but I am concerend by the return at 1 and 5 in the list. Any ideas why somone else may be using the text from my site, and how can I make an abuse report? Thanks
2
2583
by: Frances Del Rio | last post by:
http://www.emol.com/especiales/cocina_chilena/comida.asp this page is so neat (goes in pop-up..) how was this done? how do you give a layer (or div, I guess) a semi-transparency so you can still see img under it? (and how do you do the scroll for the div?) I know HTML very well, I also CSS and JavaScript, but am a bit mystified here... PLEASE respond by e-mail, fdr58@yahoo.com, my access to usenet is very limited.. thank you..
4
1980
by: | last post by:
Developing, building, and testing. How do it the best? Learning from the world leader - Microsoft I'm very interested in how the developing/build/testing workflow @ Microsoft looks like. I think Microsoft as world leader in software developing business must have a very good workflow. Does anyone know how the development flow @ Microsoft looks like?
4
2384
by: Supra | last post by:
in vb6 listbox1.remove item 5 how will i do in vb.net? regards
7
2578
by: anushhprabu | last post by:
#define q(k)main(){ return!puts(#k"\nPRABUq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) guys i'm working on this code.. i got it fromnet.. how this is working.. anyone pls.. it is printing the same content on screen.. how ya.. how it is.. how how how?? please...................
0
1985
by: candra | last post by:
Learn What Hackers Know? -General Hacking Information -Password Security -Scanning, Fingerprinting And Similar Techniques -How Hackers Attack Numerous Internet Services -How Hackers Attack Web Servers, Cgis, PHP, ASP, Etc -How Hackers Attack IRC, Instant Messaging, And Multiplayer Games -Vulnerabilities Found In Platforms With Smaller Market Share -How Hackers Attack Novell And Networks
2
1616
by: belred | last post by:
i just read this blog about how many objects (types) are loaded for a hello world program in C#. http://blogs.msdn.com/abhinaba/archive/2008/09/15/how-many-types-are-loaded-for-hello-world.aspx how can you find out how many are loaded for a python program: print 'hello'
0
8867
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9158
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.