您现在的位置: 365建站网 > 365文章 > 以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc

以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc

文章来源:365jz.com     点击数:451    更新时间:2009-09-13 10:53   参与评论

 

/// <summary>
    /// 将以树型目录化的数据通过递归转换成xml格式的数据绑定到Flex软件中的树型目录中
    /// </summary>
    public XmlDocument FillTreeViewOfXmlDocParam(string strReportType_Id, string strReportType_ParentId, string strReportType_Name, string strReportType_ClassType, string strChkReport, string strTableName, decimal iRoot, string strWhere)
    {
        XmlDocument xmldoc = new XmlDocument();
        XmlNode xmlelem_chart = null;
        XmlNode xmlnode_CurrP = null;
        XmlNode xmlnode_Sun_Emp = null;
        XmlNode xmlnode_Sun = null;
        return GetXmlDocOfAddXmlParam(xmldoc, xmlelem_chart, xmlnode_CurrP, xmlnode_Sun_Emp, xmlnode_Sun, strReportType_Id, strReportType_ParentId, strReportType_Name, strReportType_ClassType, strChkReport, strTableName, iRoot, strWhere);
    }
   
    /// <summary>
    /// 将以树型目录化的数据通过递归转换成xml格式的数据绑定到Flex软件中的树型目录中
    /// </summary>
    /// <param name="xmldoc">XmlDocument对象</param>
    /// <param name="xmlelem_chart">XmlNode对象</param>
    /// <param name="xmlnode_CurrP">XmlNode对象</param>
    /// <param name="xmlnode_Sun_Emp">XmlNode对象</param>
    /// <param name="xmlnode_Sun">XmlNode对象</param>
    /// <param name="iRoot">根的父节点</param>
    /// <returns></returns>
    private XmlDocument GetXmlDocOfAddXmlParam(XmlDocument xmldoc, XmlNode xmlelem_chart, XmlNode xmlnode_CurrP, XmlNode xmlnode_Sun_Emp, XmlNode xmlnode_Sun, string strReportType_Id, string strReportType_ParentId, string strReportType_Name, string strReportType_ClassType, string strChkReport, string strTableName, decimal iRoot, string strWhere)
    {
        if (xmldoc.DocumentElement == null)
        {
            XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            xmldoc.InsertBefore(xmldec, xmldoc.DocumentElement);

            string strSql = " SELECT distinct " + strReportType_Id;
            strSql += " ," + strReportType_ParentId;
            strSql += " ," + strReportType_Name;
            strSql += " ," + strReportType_ClassType;
            strSql += " ," + strChkReport;
            strSql += "  FROM " + strTableName;
            strSql += " where 1=1 and " + strReportType_ParentId;
            strSql += " =" + iRoot;
            strSql += " " + strWhere;

            DataSet ds = RtnDataSet(strSql);
            if (ds.Tables[0].Rows.Count == 0)
            {
                return xmldoc;
            }
            DataView dview = ds.Tables[0].DefaultView;
            foreach (DataRowView row in dview)
            {
                //将要传递的父级节点的值
                dPartId = decimal.Parse(row[strReportType_Id].ToString());
                strName = "x" + row[strReportType_Id].ToString();

                //产生xml文件的根元素
                xmlelem_chart = xmldoc.CreateElement("x" + row[strReportType_Id].ToString());
                //XmlNode root = xmldoc.DocumentElement;
                XmlNode NewAtt;
                //属性id
                NewAtt = xmldoc.CreateAttribute("Id");
                NewAtt.Value = row[strReportType_Id].ToString();
                xmlelem_chart.Attributes.SetNamedItem(NewAtt);
                //属性Name
                NewAtt = xmldoc.CreateAttribute("Name");
                NewAtt.Value = row[strReportType_Name].ToString();
                xmlelem_chart.Attributes.SetNamedItem(NewAtt);
                //属性ParentId
                NewAtt = xmldoc.CreateAttribute("ParentId");
                NewAtt.Value = row[strReportType_ParentId].ToString();
                xmlelem_chart.Attributes.SetNamedItem(NewAtt);
                //属性ClassType
                NewAtt = xmldoc.CreateAttribute("ClassType");
                NewAtt.Value = row[strReportType_ClassType].ToString();
                xmlelem_chart.Attributes.SetNamedItem(NewAtt);
                //添加根元素
                xmldoc.AppendChild(xmlelem_chart);
                GetXmlDocOfAddXmlParam(xmldoc, xmlelem_chart, xmlnode_CurrP, xmlnode_Sun_Emp, xmlnode_Sun, strReportType_Id, strReportType_ParentId, strReportType_Name, strReportType_ClassType, strChkReport, strTableName, iRoot, strWhere);
            }
        }
        else
        {
            string strSql = null;
            //获取根节点,当为根节点
            if (xmldoc.LastChild.Name == strName)
            {
                xmlnode_Sun_Emp = xmlelem_chart;
                xmlnode_CurrP = xmlnode_Sun_Emp;
            }
            else
            {
                xmlnode_Sun_Emp = xmlnode_Sun;
                xmlnode_CurrP = xmlnode_Sun_Emp;
            }
            strSql = "SELECT ";
            strSql += strReportType_Id;
            strSql += "," + strReportType_ParentId;
            strSql += "," + strReportType_Name;
            strSql += "," + strReportType_ClassType;
            strSql += "," + strChkReport;
            strSql += " FROM " + strTableName;
            strSql += " where 1=1 and " + strReportType_ParentId;
            strSql += " =" + "'" + dPartId + "'";
            strSql += " " + strWhere;

            DataSet ds = RtnDataSet(strSql);
            if (ds.Tables[0].Rows.Count == 0)
            {
                xmlnode_CurrP = xmlnode_Sun_Emp.ParentNode;
                booli = false;//判断是否还有下级节点
                return xmldoc;
            }
            else
            {
                booli = true;
                DataView dview = ds.Tables[0].DefaultView;
                XmlNode NewAtt;
                //xmlelem_Curr = xmlelem;
                foreach (DataRowView row in dview)
                {
                    //要传递的ReportType_ParentId,用以区别父节点
                    dPartId = decimal.Parse(row[strReportType_Id].ToString());
                    //要传递的Name,用以区别父节点
                    strName = "x" + row[strReportType_Id].ToString();

                    //创建节点,并设置节点值
                    xmlnode_Sun = xmldoc.CreateElement("x" + row[strReportType_Id].ToString());
                    //属性id
                    NewAtt = xmldoc.CreateAttribute("Id");
                    NewAtt.Value = row[strReportType_Id].ToString();
                    xmlnode_Sun.Attributes.SetNamedItem(NewAtt);
                    //属性Name
                    NewAtt = xmldoc.CreateAttribute("Name");
                    NewAtt.Value = row[strReportType_Name].ToString();
                    xmlnode_Sun.Attributes.SetNamedItem(NewAtt);
                    //属性ParentId
                    NewAtt = xmldoc.CreateAttribute("ParentId");
                    NewAtt.Value = row[strReportType_ParentId].ToString();
                    xmlnode_Sun.Attributes.SetNamedItem(NewAtt);
                    //属性ClassType
                    NewAtt = xmldoc.CreateAttribute("ClassType");
                    NewAtt.Value = row[strReportType_ClassType].ToString();
                    xmlnode_Sun.Attributes.SetNamedItem(NewAtt);

                    xmlnode_CurrP.AppendChild(xmlnode_Sun);
                    if (booli == true)
                    {
                        xmlnode_Sun_Emp = xmlnode_CurrP;
                    }
                    //如果子节点还有子节点,则递归
                    GetXmlDocOfAddXmlParam(xmldoc, xmlelem_chart, xmlnode_CurrP, xmlnode_Sun_Emp, xmlnode_Sun, strReportType_Id, strReportType_ParentId, strReportType_Name, strReportType_ClassType, strChkReport, strTableName, iRoot, strWhere);
                }
            }
        }
        return xmldoc;

    }

 

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

您可能感兴趣的文章:

发表评论 (451人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号