Skip to content

Commit 923b73d

Browse files
committed
Added the Xml commands.
1 parent b2cc5ec commit 923b73d

File tree

1 file changed

+18
-10
lines changed
  • Mail-Merge/Generate-invoices-from-xml-data/.NET/Generate-invoices-from-xml-data

1 file changed

+18
-10
lines changed

Mail-Merge/Generate-invoices-from-xml-data/.NET/Generate-invoices-from-xml-data/Program.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,72 @@ public static void Main(string[] args)
1010
{
1111
// Load the template Word document
1212
WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"));
13-
// To start each record at new page, so enable this property
13+
// start each record at new page
1414
document.MailMerge.StartAtNewPage = true;
15-
// Perform the mail merge for the group
15+
// Perform mail merge using relational XML data
1616
document.MailMerge.ExecuteNestedGroup(GetRelationalData());
1717
// Save the result Word document.
1818
document.Save(Path.GetFullPath("../../../Output/Output.docx"));
1919
// Close the Word document
2020
document.Close();
2121
}
2222
#region Helper Method
23-
static MailMergeDataTable GetRelationalData()
23+
/// <summary>
24+
/// Retrieves relational invoice data from XML and converts it into a MailMergeDataTable.
25+
/// </summary>
26+
private static MailMergeDataTable GetRelationalData()
2427
{
25-
//Gets data from XML
28+
// Load the XML data file
2629
Stream xmlStream = File.OpenRead(Path.GetFullPath(@"Data/InvoiceDetails.xml"));
2730
XmlDocument xmlDocument = new XmlDocument();
2831
xmlDocument.Load(xmlStream);
2932
xmlStream.Dispose();
3033

3134
ExpandoObject customerDetails = new ExpandoObject();
3235
GetDataAsExpandoObject((xmlDocument as XmlNode).LastChild, ref customerDetails);
33-
// Treat customerDetails as a dictionary
36+
// Convert dynamic object into dictionary
3437
IDictionary<string, object> customerDict = customerDetails as IDictionary<string, object>;
3538
// Get the "Invoices" list
3639
List<ExpandoObject> invoicesList = customerDict["Invoices"] as List<ExpandoObject>;
3740
// Take the first item in that list
3841
IDictionary<string, object> firstInvoiceGroup = invoicesList[0] as IDictionary<string, object>;
3942
// Get the "Invoice" list from that group
40-
List<ExpandoObject> invoices = firstInvoiceGroup["Invoice"] as List<ExpandoObject>;
41-
//Creates an instance of "MailMergeDataTable" by specifying mail merge group name and "IEnumerable" collection
43+
List<ExpandoObject> invoices = firstInvoiceGroup["Invoice"] as List<ExpandoObject>;
44+
// Create MailMergeDataTable with group name "Invoices"
4245
MailMergeDataTable dataTable = new MailMergeDataTable("Invoices", invoices);
4346
return dataTable;
4447
}
4548
/// <summary>
4649
/// Gets the data as ExpandoObject.
4750
/// </summary>
48-
/// <param name="reader">The reader.</param>
51+
/// <param name="node">The current XML node being processed.</param>
52+
/// <param name="dynamicObject">The dynamic object to populate with node data.</param>
4953
/// <returns></returns>
50-
/// <exception cref="System.Exception">reader</exception>
51-
/// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception>
5254
private static void GetDataAsExpandoObject(XmlNode node, ref ExpandoObject dynamicObject)
5355
{
5456
if (node.InnerText == node.InnerXml)
57+
// Leaf node: store text value
5558
dynamicObject.TryAdd(node.LocalName, node.InnerText);
5659
else
5760
{
61+
// Handle child nodes
5862
List<ExpandoObject> childObjects;
63+
// If the tag already exists, reuse the list; otherwise create a new one
5964
if ((dynamicObject as IDictionary<string, object>).ContainsKey(node.LocalName))
6065
childObjects = (dynamicObject as IDictionary<string, object>)[node.LocalName] as List<ExpandoObject>;
6166
else
6267
{
6368
childObjects = new List<ExpandoObject>();
6469
dynamicObject.TryAdd(node.LocalName, childObjects);
6570
}
71+
// Create a new child object for the current node
6672
ExpandoObject childObject = new ExpandoObject();
73+
// Recursively process all child nodes
6774
foreach (XmlNode childNode in (node as XmlNode).ChildNodes)
6875
{
6976
GetDataAsExpandoObject(childNode, ref childObject);
7077
}
78+
// Add the processed child object to the list
7179
childObjects.Add(childObject);
7280
}
7381
}

0 commit comments

Comments
 (0)