`
zzc125
  • 浏览: 12881 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

将数据库中的记录转换为XML文档

阅读更多

因为XML的通用传输性好,在程序开发中很多地方都会用到XML文档,比如配置文件,数据传输文件,语言文件等等。一直以来觉得把数据库中的记录转换成一个XML文档对我来说很有难度,今天也是被项目的一个BUG逼急了,冷静下来并参考了同事的建议,终于完成了将数据库记录转换为XML文档的目的。

首先准备数据库Test2008,SQL脚本如下:

create table ClassCategory
(
ID int identity(1,1) not null,
Class_ID varchar(50) not null,
CreateOn datetime default getdate()
)

alter table ClassCategory
add  ClassName varchar(200)

insert into ClassCategory(Class_ID,ClassName)values('3167382','三一班');
insert into ClassCategory(Class_ID,ClassName)values('3267382','三二班');

select * from ClassCategory

Create Table ClassInfo
(
	ID int identity(1,1) not null,
	Class_ID varchar(50) not null,
	Class_Title varchar(Max) not null,
	Grade_ID varchar(50) not null,
	CreateOn datetime default getdate()
)

insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3167382','测试数据','3167382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');
insert into ClassInfo(Class_ID,Class_Title,Grade_ID)values('3267382','测试数据','3267382');


select * from ClassInfo


Create Procedure GetClassCategory
as
begin
	select * from ClassCategory
end


create Proc GetClassInfo
as 
begin
	select * from ClassInfo
end

需要实现的效果像这样的:

22222

我封装了一个从数据库中获取数据的DAL:

  public class XMLOperatoer
    {
        private static string ConnectionString = "Server=Liszt-PC\\SQLExpress;database=Test2008;uid=sa;pwd=123";
        public static  DataTable GetSecondNode()
        {
            DataTable dt = MyBlog.SqlHelper.ExecuteDataset(ConnectionString, "GetClassCategory", null).Tables[0];
            return dt;
        }

        public static  DataTable GetThreedNode()
        {
            DataTable dt = MyBlog.SqlHelper.ExecuteDataset(ConnectionString, "GetClassInfo", null).Tables[0];
            return dt;
        }
    }


首先需要创建一个XML文档,然后新建第二级的元素:

 XmlDocument doc = new XmlDocument();
        doc.LoadXml("<classinfo name='\"Liszt\"'></classinfo>");

        DataTable dt = Liszt.DAL.XMLOperatoer.GetSecondNode();

        foreach (DataRow item in dt.Rows)
        {
            XmlElement el = doc.CreateElement("Class");
            el.SetAttribute("ID", item["ID"].ToString());
            el.SetAttribute("Class_ID", item["Class_ID"].ToString());
            el.SetAttribute("ClassName", item["ClassName"].ToString());

            //el.InnerText = item["ClassName"].ToString();
            doc.DocumentElement.AppendChild(el);
        }
        XmlNodeList nodesID = doc.SelectNodes("ClassInfo/Class//@Class_ID");

这样就创建好了第二级的元素,可以通过doc.InnerXml来查看生成的XML文档的内容。然后通过关联的Class_ID来创建第三层的元素:

  //选择含有Class_ID属性的元素
        XmlNodeList nodesID = doc.SelectNodes("ClassInfo/Class//@Class_ID");
        
        DataTable dtThreed = Liszt.DAL.XMLOperatoer.GetThreedNode();
        foreach (DataRow item in dtThreed.Rows)
        {
            for (int i = 0; i 



这样就创建好了第三层元素了。

最后可以来看看结果:

<classinfo name="&lt;span style=" color:>Liszt"&gt;
    <class id="&lt;span style=" color:>1" Class_ID="<span style="color: #8b0000">3238933</span>" ClassName="<span style="color: #8b0000">三年级</span>"&gt;
        <class id="&lt;span style=" color:>1" Class_ID="<span style="color: #8b0000">3238933</span>" Class_Title="<span style="color: #8b0000">三一班</span>" Grade_ID="<span style="color: #8b0000">3238933</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
        <class id="&lt;span style=" color:>2" Class_ID="<span style="color: #8b0000">3238933</span>" Class_Title="<span style="color: #8b0000">三二版</span>" Grade_ID="<span style="color: #8b0000">3267382</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
        <class id="&lt;span style=" color:>5" Class_ID="<span style="color: #8b0000">3238933</span>" Class_Title="<span style="color: #8b0000">三三版</span>" Grade_ID="<span style="color: #8b0000">3267382</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
    </class><class id="&lt;span style=" color:>2" Class_ID="<span style="color: #8b0000">2267382</span>" ClassName="<span style="color: #8b0000">二年级</span>"&gt;
        <class id="&lt;span style=" color:>3" Class_ID="<span style="color: #8b0000">2267382</span>" Class_Title="<span style="color: #8b0000">二三班</span>" Grade_ID="<span style="color: #8b0000">3238933</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
        <class id="&lt;span style=" color:>4" Class_ID="<span style="color: #8b0000">2267382</span>" Class_Title="<span style="color: #8b0000">二五班</span>" Grade_ID="<span style="color: #8b0000">3267382</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
        <class id="&lt;span style=" color:>6" Class_ID="<span style="color: #8b0000">2267382</span>" Class_Title="<span style="color: #8b0000">二七班</span>" Grade_ID="<span style="color: #8b0000">3238933</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
        <class id="&lt;span style=" color:>7" Class_ID="<span style="color: #8b0000">2267382</span>" Class_Title="<span style="color: #8b0000">二九班</span>" Grade_ID="<span style="color: #8b0000">3267382</span>" CreateOn="<span style="color: #8b0000">2011/3/29 22:47:23</span>" /&gt;
    </class></class></class></class></class></class></class></class></classinfo>

源代码下载
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics