Hi everybody.
recently I was working in a project where the requirement was save "trdx" files into a DB and show them in a asp.net web form application. As additional information, the data to populate the reports were in XML files.
so I realized to late that "Telerik reporting" doesn't accept xml as datasource or at least I couldn't find a direct way to achieve that. I read that you can transform the xml into objectdatasource but it was little complicated due the dynamism in the project.
at the end I decided to work with a user function that basically accept two parameters
- XPathExpression => the xpath query to find the data in the xml string
- xmlDocument => the xml string, I pass it to my report by a parameter.
The function :
[Function(Category = "XPath", Namespace = "XmlTelerikReporting", Description = "Find the information from the default report parameter XmlDataSource using XPath expressions ")] public static object XmlValue(string XPathExpression, string xmlDocument) { #region XmlValue try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlDocument); xmlDoc.DocumentElement.RemoveAttribute("xmlns"); XmlDocument newDom = new XmlDocument(); newDom.LoadXml(System.Text.RegularExpressions.Regex.Replace( xmlDoc.OuterXml , @"(xmlns:?[^=]*=[""][^""]*[""])", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline)); object xmlXPathValue = newDom.SelectSingleNode(XPathExpression) != null ? newDom.SelectSingleNode(XPathExpression).Value : null; return xmlXPathValue; } catch (Exception ex) { //a pretty ans secret way to catch exceptions } #endregion }
How to use in expression:
= XmlTelerikReporting.XmlValue( "//@parametertofind" , Parameters.xmlDocument.Value )
I will really appreciate any feedback regarding to my code display above.
and it will be awesome if you implement something in telerik reporting in order to accept XML files as Datasource
Regards!