Posted 5 September 2019, 12:22 am EST - Updated 30 September 2022, 8:34 am EST
How should I create a XML data source in order to allow repeating elements to work correctly? Currently I am creating the XML data source like this
private DataObject GetXMLDataSource(ReportPreviewOptions reportPreviewOptions) {
string xmlData = Encoding.UTF8.GetString(Convert.FromBase64String(reportPreviewOptions.Parameters)); // The parameters are received from the front end
DataSource dataSource = new DataSource();
dataSource.Name = "XML Data Source";
dataSource.ConnectionProperties.DataProvider = "XML";
dataSource.ConnectionProperties.ConnectString = $"xmldata={xmlData}";
DataSet dataSet = new DataSet();
XDocument xmlDocument = XDocument.Parse(xmlData);
dataSet.Name = dataSource.Name;
dataSet.Query.CommandType = QueryCommandType.Text;
XElement recordItem = xmlDocument.Descendants(reportPreviewOptions.XMLRecordTag).FirstOrDefault();
dataSet.Query.CommandText = (recordItem).GetAbsoluteXPath().Replace("[1]", ""); //CommandText evaluates to "\people\person"
dataSet.Query.DataSourceName = dataSource.Name;
foreach (XName name in xmlDocument.Root.DescendantNodes().OfType<XElement>().Select(x => x.Name).Distinct()) {
XElement xElement = xmlDocument.Descendants(name).FirstOrDefault();
//This loops each distinct node of the XML structure and returns the XPath to the node i.e. /people/person/name/given
if (xElement == null) {
throw new Exception();
}
Field field = new Field();
string xmlPath = xElement.GetAbsoluteXPath().Replace("[1]", "");
field.DataField = xmlPath;
field.Name = name.LocalName;
dataSet.Fields.Add(field);
}
return new DataObject() { DataSet = dataSet, DataSource = dataSource };
}
And this results in a datasource like this on the designer
So it looks great in the designer and is easy to map but when I preview the data I get the wrong information. This is the sample XML I am using taken from the AR documentation ```
John
Doe
Jane
Smith
So based on that XML I am expecting two rows with different details. But instead I am getting two rows repeating the first row details as seen below
[img]https://gccontent.blob.core.windows.net/forum-uploads/file-023cd2f3-6b0e-47c0-bbcc-c1730287938c.PNG[/img]