以树型目录化的数据库表数据通过递归转换成xml格式的xmldoc
/// <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;
}