原文:
将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+"不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}