根据MSSQL中的列记录将表转换为XML [英] Convert a table to XML based on column records in MSSQL

查看:64
本文介绍了根据MSSQL中的列记录将表转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含目录路径的表,有些像这样的



I have a Table that contains directory path some what like this

id  ParentId    Path                            type       Desc
1    -1         Root                              0         a
2     1         Root/Folder1                      0         b
3     1         Root/Folder2                      0         c
4     2         Root/Folder1/abc.dox              1         d
5     3         Root/Folder2/pqr.doc              1         e
6     1         Root/folder3                      0         f
7     6         Root/Folder3/asd.doc              1         g



我想要像这样创建xml



< Root>

< Folder1 id =2desc =b/>

< abc id =4desc =d/>

< Folder2 id =3desc =3/>

< pqr id =5desc =e/>







< / Root>



大约有1000条记录,层次结构不是固定类型,表示0 - 表示文件夹,1表示文件


I want to create xml like this

<Root>
<Folder1 id="2" desc="b"/>
<abc id="4" desc="d"/>
<Folder2 id="3" desc="3"/>
<pqr id="5" desc="e"/>
.
.
.
</Root>

There are around 1000 records and hierarchy is not fixed type indicates 0 - for folder and 1 -for file

推荐答案

DataTable table = new DataTable( ){TableName =Customer};



DataColumn keyColumn = table.Columns.Add(id,typeof(System.Int32));

table.Columns.Add(ParentId,typeof(System.Int32));

table.Columns.Add(Path,typeof(System.String));

table.coulmn.Add(type,typeof(System.Char));

table.coulmn.Add(Desc,typeof(System.Char));



table.PrimaryKey = new DataColumn [] {keyColumn};



table.AcceptChanges();





DataTable table = new DataTable() { TableName = "Customer" };

DataColumn keyColumn = table.Columns.Add("id", typeof(System.Int32));
table.Columns.Add("ParentId", typeof(System.Int32));
table.Columns.Add("Path", typeof(System.String));
table.coulmn.Add("type",typeof(System.Char));
table.coulmn.Add("Desc", typeof(System.Char));

table.PrimaryKey = new DataColumn[] { keyColumn };

table.AcceptChanges();


string xmlString = string.Empty;
using (TextWriter writer = new StringWriter())
{
  table.WriteXml(writer);
  xml = writer.ToString();
}


查看 LINK [ ^ ]


这篇关于根据MSSQL中的列记录将表转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆