我想将DataTable嵌套到另一个DataTable中,如下所示:
<Mail>
<SMTP>
<Server>smtp.myserver.de</Server>
<Port>25</Port>
<Security>STARTTLS</Security>
<Authmethod>Login</Authmethod>
</SMTP>
<IMAP>
<Server>imap.myserver.de</Server>
<Port>993</Port>
<Security>STARTTLS</Security>
<Authmethod>Login</Authmethod>
</IMAP>
<Username>myusername</Username>
<Password>mypassword</Password>
<Mailaddress>me@myserver.de</Mailaddress>
</Mail>
我的代码:
DataTable dt = new DataTable();
DataTable dtsmtp = new DataTable();
DataTable dtimap = new DataTable();
dt.TableName = "Mail";
dtsmtp.TableName = "SMTP";
dtimap.TableName = "IMAP";
DataColumn dcsmtp1 = new DataColumn("Server");
DataColumn dcsmtp2 = new DataColumn("Port");
DataColumn dcsmtp3 = new DataColumn("Security");
DataColumn dcsmtp4 = new DataColumn("Authmethod");
DataColumn dcimap1 = new DataColumn("Server");
DataColumn dcimap2 = new DataColumn("Port");
DataColumn dcimap3 = new DataColumn("Security");
DataColumn dcimap4 = new DataColumn("Authmethod");
DataColumn dc1 = new DataColumn("Username");
DataColumn dc2 = new DataColumn("Password");
DataColumn dc3 = new DataColumn("Mailaddress");
dtsmtp.Columns.Add(dcsmtp1);
dtsmtp.Columns.Add(dcsmtp2);
dtsmtp.Columns.Add(dcsmtp3);
dtsmtp.Columns.Add(dcsmtp4);
dtimap.Columns.Add(dcimap1);
dtimap.Columns.Add(dcimap2);
dtimap.Columns.Add(dcimap3);
dtimap.Columns.Add(dcimap4);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dtsmtp.Rows.Add("smtp.myserver.de", "25", "STARTTLS", "Password");
dtimap.Rows.Add("imap.myserver.de", "993", "STARTTLS", "Password");
dt.Rows.Add(dtsmtp);
dt.Rows.Add(dtimap);
dt.Rows.Add("myusername", "mypassword", "me@myserver.de");
ds.Tables.Add(dt);
如果我做对了,将表“SMTP”和“IMAP”添加到我的主表“Mail”就足够了。不幸的是它不起作用,我只得到:
<Mail>
<Username>SMTP</Username>
</Mail-Configuration>
<Mail-Configuration>
<Username>IMAP</Username>
</Mail>
<Mail>
<Username>myusername</Username>
<Password>mypassword</Password>
<Mailaddress>me@myserver.de</Mailaddress>
</Mail>
请有人指出我正确的方向。非常感谢!
修改 抱歉!首先,我应该提到我使用WriteXml()将其转换为XML格式。 在我插入关系后,我得到了:
<Mail>
<Username>myusername</Username>
<Password>mypassword</Password>
<Mailaddress>me@myserver.de</Mailaddress>
<smtp_id>Mail_SMTP</smtp_id>
<imap_id>Mail_IMAP</imap_id>
</Mail>
<SMTP>
<Server>smtp.myserver.de</Server>
<Port>25</Port>
<Security>STARTTLS</Security>
<Authmethod>Login</Authmethod>
</SMTP>
<IMAP>
<Server>imap.myserver.de</Server>
<Port>993</Port>
<Security>STARTTLS</Security>
<Authmethod>Login</Authmethod>
</IMAP>
所以,我们很接近,但还不够近......
代码:
ds.Tables.Add(dt);
ds.Tables.Add(dtimap);
ds.Tables.Add(dtsmtp);
DataRelation relation = new DataRelation("Mail_SMTP", dtsmtpId, dcsmtpdtId, true);
relation.Nested = true;
ds.Relations.Add(relation);
DataRelation relation1 = new DataRelation("Mail_IMAP", dtimapid, dcimapdtId, true);
relation1.Nested = true;
ds.Relations.Add(relation1);
dt.Rows.Add("myusername", "mypassword", "me@myserver.de", relation, relation1);
答案 0 :(得分:0)
如果数据表首先需要不是xml生成,那么数据表的结构不应该是这样的。应将它们放入数据集并添加关系。这确保了正确的组织。
对于xml生成,最好使用其他工具,如XDocument
class。
答案 1 :(得分:0)
您似乎正在尝试将dtsmtp
和dtimap
作为行添加到dt
数据表中。为了获得结构,我认为您需要在dt
数据表中添加smtp和imap 列,然后添加dtsmtp
和dtimap
当您添加其他数据时......
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add("smtp", typeof(DataTable));
dt.Columns.Add("imap", typeof(DataTable));
dtsmtp.Rows.Add("smtp.myserver.de", "25", "STARTTLS", "Password");
dtimap.Rows.Add("imap.myserver.de", "993", "STARTTLS", "Password");
dt.Rows.Add("myusername", "mypassword", "me@myserver.de", dtsmtp, dtimap);
我不确定您将其转换为XML的方法,但这是您想要的排序结构。
答案 2 :(得分:0)
尝试添加数据集中的所有表,并使用唯一标识列
在表之间创建关系//Create new unique column to maintain relationship between tables
DataColumn dtId = new DataColumn("Id"); //it act as primary key
dtId.ColumnMapping = MappingType.Hidden; //hide mapping column while generating xml
DataColumn dcimap_dtId = new DataColumn("Mail_Id"); // foreign key
dcimap_dtId.ColumnMapping = MappingType.Hidden;
DataColumn dcsmtp_dtId = new DataColumn("Mail_Id"); // foreign key
dcsmtp_id.ColumnMapping = MappingType.Hidden;
// add newly created columns into appropriate tables and position
...
//fill data rows as per data table column arrangement
dt.Rows.Add(1, "myusername", "mypassword", "me@myserver.de"); // 1 is primary key
dtsmtp.Rows.Add("smtp.myserver.de", "25", "STARTTLS", "Password", 1); // 1 is here act as foreign key
dtimap.Rows.Add("imap.myserver.de", "993", "STARTTLS", "Password", 1);
// add all tables inside the dataset
ds.Tables.Add(dt);
ds.Tables.Add(dtsmtp);
ds.Tables.Add(dtimap);
//create relationship between tables
DataRelation relation = new DataRelation("Mail_SMTP", dtId, dcsmtp_dtId, true);
relation.Nested = true;
ds.Relations.Add(relation);
DataRelation relation1 = new DataRelation("Mail_IMAP", dtId, dcimap_dtId, true);
relation1.Nested = true;
ds.Relations.Add(relation1);
//Write to file
ds.WriteXml("file.xml");
- SJ