加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将RDL报表转换成RDLC报表的函数

发布时间:2020-12-16 23:47:00 所属栏目:百科 来源:网络整理
导读:原文: 将RDL报表转换成RDLC报表的函数 近日研究RDLC报表,发现其不能与RDL报表兼容,尤其是将RDL报表转换成RDLC报表。网上的资料贴出的的转换方式复杂且不切实际,遂决定深入研究。经研究发现,RDL报表与RDLC报表的XML格式有些差异,将RDL报表的XML格式改成
原文: 将RDL报表转换成RDLC报表的函数

近日研究RDLC报表,发现其不能与RDL报表兼容,尤其是将RDL报表转换成RDLC报表。网上的资料贴出的的转换方式复杂且不切实际,遂决定深入研究。经研究发现,RDL报表与RDLC报表的XML格式有些差异,将RDL报表的XML格式改成与RDLC报表的XML格式相同,发现转换成功! 如需转换123.rdl文件,只需RDLConvertRDLC("123.rdl"),即可转换成123.rdlc文件。由于本人对带命名空间的XML文件操作不熟悉,不能将除根节点意外的其他节点的xmlns属性只去掉,如有高手,欢迎指教!
private void RDLConvertRDLC(string strFile)
        {
            if(File.Exists(strFile))
            {
                try
                {
                    XmlDocument xmlBak;
                    XmlNamespaceManager nsMgrBak;
                    XmlNodeList Reports;

                    // 打开需转换的XML文件
                    try
                    {
                        xmlBak = new XmlDocument();
                        xmlBak.Load(strFile);
                        nsMgrBak = new XmlNamespaceManager(xmlBak.NameTable);
                        nsMgrBak.AddNamespace("nsBak","http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition");
                        Reports = xmlBak.SelectSingleNode("/nsBak:Report",nsMgrBak).ChildNodes;
                    }
                    catch
                    {
                        File.Move(strFile,strFile + "c");
                        return;
                    }

                    // 创建新的XML文件
                    XmlDocument xmlDoc = new XmlDocument();
                    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
                    xmlDoc.AppendChild(dec);

                    // 创建一个根节点Report
                    XmlElement root = xmlDoc.CreateElement("Report");
                    root.SetAttribute("xmlns:rd","http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
                    root.SetAttribute("xmlns","http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition");
                    xmlDoc.AppendChild(root);

                    // 拷贝节点数据到新XML文件
                    for (int i = 0; i < Reports.Count; i++)
                    {
                        if (Reports[i].Name != "AutoRefresh")
                        {
                            if (Reports[i].Name == "ReportSections")
                            {
                                XmlNodeList ReportSections = xmlBak.SelectSingleNode("/nsBak:Report/nsBak:ReportSections/nsBak:ReportSection",nsMgrBak).ChildNodes;
                                for (int j = 0; j < ReportSections.Count; j++)
                                {
                                    XmlElement newElement = (XmlElement)xmlDoc.ImportNode(ReportSections[j],true);
                                    newElement.SetAttribute("xmlns","http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition");
                                    root.AppendChild(newElement);
                                }
                            }
                            else
                            {
                                XmlElement newElement = (XmlElement)xmlDoc.ImportNode(Reports[i],true);
                                newElement.SetAttribute("xmlns","http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition");
                                root.AppendChild(newElement);
                            }
                        }
                    }
                    xmlDoc.Save(@strFile + "c");
                    File.Delete(strFile);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(),"错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("文件"+strFile+"不存在!",MessageBoxIcon.Information);
            }
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读