Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36518.9 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generate-invoices-from-xml-data", "Generate-invoices-from-xml-data\Generate-invoices-from-xml-data.csproj", "{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {74001C0A-D635-46A4-AF4A-401D245EF6AA}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Invoices>
<Invoice>
<InvoiceNumber>12345</InvoiceNumber>
<Date>2025-04-28</Date>
<CustomerName>John Doe</CustomerName>
<Products>
<Position>1</Position>
<ProductSku>SKU123</ProductSku>
<ProductName>Product A</ProductName>
<Price>100.00</Price>
</Products>
<Products>
<Position>2</Position>
<ProductSku>SKU124</ProductSku>
<ProductName>Product B</ProductName>
<Price>200.00</Price>
</Products>
<Products>
<Position>3</Position>
<ProductSku>SKU125</ProductSku>
<ProductName>Product C</ProductName>
<Price>200.00</Price>
</Products>
<TotalAmount>500.00</TotalAmount>
</Invoice>
<Invoice>
<InvoiceNumber>10295</InvoiceNumber>
<Date>2025-04-20</Date>
<CustomerName>Paul Henriot</CustomerName>
<Products>
<Position>1</Position>
<ProductSku>SKU123</ProductSku>
<ProductName>Product D</ProductName>
<Price>150.00</Price>
</Products>
<Products>
<Position>2</Position>
<ProductSku>SKU124</ProductSku>
<ProductName>Product E</ProductName>
<Price>200.00</Price>
</Products>
<Products>
<Position>3</Position>
<ProductSku>SKU125</ProductSku>
<ProductName>Product F</ProductName>
<Price>220.00</Price>
</Products>
<TotalAmount>570.00</TotalAmount>
</Invoice>
</Invoices>

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Generate_invoices_from_xml_data</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>


<ItemGroup>
<None Update="Data\InvoiceDetails.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Syncfusion.DocIO.DLS;
using System.Dynamic;
using System.Xml;

namespace Generate_invoices_from_xml_data
{
class Program
{
public static void Main(string[] args)
{
// Load the template Word document
WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"));
// To start each record at new page, so enable this property
document.MailMerge.StartAtNewPage = true;
// Perform the mail merge for the group
document.MailMerge.ExecuteNestedGroup(GetRelationalData());
// Save the result Word document.
document.Save(Path.GetFullPath("../../../Output/Output.docx"));
// Close the Word document
document.Close();
}
#region Helper Method
static MailMergeDataTable GetRelationalData()
{
//Gets data from XML
Stream xmlStream = File.OpenRead(Path.GetFullPath(@"Data/InvoiceDetails.xml"));
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlStream);
xmlStream.Dispose();

ExpandoObject customerDetails = new ExpandoObject();
GetDataAsExpandoObject((xmlDocument as XmlNode).LastChild, ref customerDetails);
// Treat customerDetails as a dictionary
IDictionary<string, object> customerDict = customerDetails as IDictionary<string, object>;
// Get the "Invoices" list
List<ExpandoObject> invoicesList = customerDict["Invoices"] as List<ExpandoObject>;
// Take the first item in that list
IDictionary<string, object> firstInvoiceGroup = invoicesList[0] as IDictionary<string, object>;
// Get the "Invoice" list from that group
List<ExpandoObject> invoices = firstInvoiceGroup["Invoice"] as List<ExpandoObject>;
//Creates an instance of "MailMergeDataTable" by specifying mail merge group name and "IEnumerable" collection
MailMergeDataTable dataTable = new MailMergeDataTable("Invoices", invoices);
return dataTable;
}
/// <summary>
/// Gets the data as ExpandoObject.
/// </summary>
/// <param name="reader">The reader.</param>
/// <returns></returns>
/// <exception cref="System.Exception">reader</exception>
/// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception>
private static void GetDataAsExpandoObject(XmlNode node, ref ExpandoObject dynamicObject)
{
if (node.InnerText == node.InnerXml)
dynamicObject.TryAdd(node.LocalName, node.InnerText);
else
{
List<ExpandoObject> childObjects;
if ((dynamicObject as IDictionary<string, object>).ContainsKey(node.LocalName))
childObjects = (dynamicObject as IDictionary<string, object>)[node.LocalName] as List<ExpandoObject>;
else
{
childObjects = new List<ExpandoObject>();
dynamicObject.TryAdd(node.LocalName, childObjects);
}
ExpandoObject childObject = new ExpandoObject();
foreach (XmlNode childNode in (node as XmlNode).ChildNodes)
{
GetDataAsExpandoObject(childNode, ref childObject);
}
childObjects.Add(childObject);
}
}
#endregion
}
}

Loading