|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using System.Dynamic; |
| 3 | +using System.Xml; |
| 4 | + |
| 5 | +namespace Generate_invoices_from_xml_data |
| 6 | +{ |
| 7 | + class Program |
| 8 | + { |
| 9 | + public static void Main(string[] args) |
| 10 | + { |
| 11 | + // Load the template Word document |
| 12 | + WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")); |
| 13 | + // start each record at new page |
| 14 | + document.MailMerge.StartAtNewPage = true; |
| 15 | + // Perform mail merge using relational XML data |
| 16 | + document.MailMerge.ExecuteNestedGroup(GetRelationalData()); |
| 17 | + // Save the result Word document. |
| 18 | + document.Save(Path.GetFullPath("../../../Output/Output.docx")); |
| 19 | + // Close the Word document |
| 20 | + document.Close(); |
| 21 | + } |
| 22 | + #region Helper Method |
| 23 | + /// <summary> |
| 24 | + /// Retrieves relational invoice data from XML and converts it into a MailMergeDataTable. |
| 25 | + /// </summary> |
| 26 | + private static MailMergeDataTable GetRelationalData() |
| 27 | + { |
| 28 | + // Load the XML data file |
| 29 | + Stream xmlStream = File.OpenRead(Path.GetFullPath(@"Data/InvoiceDetails.xml")); |
| 30 | + XmlDocument xmlDocument = new XmlDocument(); |
| 31 | + xmlDocument.Load(xmlStream); |
| 32 | + xmlStream.Dispose(); |
| 33 | + |
| 34 | + ExpandoObject customerDetails = new ExpandoObject(); |
| 35 | + GetDataAsExpandoObject((xmlDocument as XmlNode).LastChild, ref customerDetails); |
| 36 | + // Convert dynamic object into dictionary |
| 37 | + IDictionary<string, object> customerDict = customerDetails as IDictionary<string, object>; |
| 38 | + // Get the "Invoices" list |
| 39 | + List<ExpandoObject> invoicesList = customerDict["Invoices"] as List<ExpandoObject>; |
| 40 | + // Take the first item in that list |
| 41 | + IDictionary<string, object> firstInvoiceGroup = invoicesList[0] as IDictionary<string, object>; |
| 42 | + // Get the "Invoice" list from that group |
| 43 | + List<ExpandoObject> invoices = firstInvoiceGroup["Invoice"] as List<ExpandoObject>; |
| 44 | + // Create MailMergeDataTable with group name "Invoices" |
| 45 | + MailMergeDataTable dataTable = new MailMergeDataTable("Invoices", invoices); |
| 46 | + return dataTable; |
| 47 | + } |
| 48 | + /// <summary> |
| 49 | + /// Gets the data as ExpandoObject. |
| 50 | + /// </summary> |
| 51 | + /// <param name="node">The current XML node being processed.</param> |
| 52 | + /// <param name="dynamicObject">The dynamic object to populate with node data.</param> |
| 53 | + /// <returns></returns> |
| 54 | + private static void GetDataAsExpandoObject(XmlNode node, ref ExpandoObject dynamicObject) |
| 55 | + { |
| 56 | + if (node.InnerText == node.InnerXml) |
| 57 | + // Leaf node: store text value |
| 58 | + dynamicObject.TryAdd(node.LocalName, node.InnerText); |
| 59 | + else |
| 60 | + { |
| 61 | + // Handle child nodes |
| 62 | + List<ExpandoObject> childObjects; |
| 63 | + // If the tag already exists, reuse the list; otherwise create a new one |
| 64 | + if ((dynamicObject as IDictionary<string, object>).ContainsKey(node.LocalName)) |
| 65 | + childObjects = (dynamicObject as IDictionary<string, object>)[node.LocalName] as List<ExpandoObject>; |
| 66 | + else |
| 67 | + { |
| 68 | + childObjects = new List<ExpandoObject>(); |
| 69 | + dynamicObject.TryAdd(node.LocalName, childObjects); |
| 70 | + } |
| 71 | + // Create a new child object for the current node |
| 72 | + ExpandoObject childObject = new ExpandoObject(); |
| 73 | + // Recursively process all child nodes |
| 74 | + foreach (XmlNode childNode in (node as XmlNode).ChildNodes) |
| 75 | + { |
| 76 | + GetDataAsExpandoObject(childNode, ref childObject); |
| 77 | + } |
| 78 | + // Add the processed child object to the list |
| 79 | + childObjects.Add(childObject); |
| 80 | + } |
| 81 | + } |
| 82 | + #endregion |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments