Skip to content

Commit d53e250

Browse files
committed
Added Sample
1 parent e5e292f commit d53e250

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{DE5C1DFA-6686-4D2C-878A-B0BC60558FF8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {74001C0A-D635-46A4-AF4A-401D245EF6AA}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Invoices>
3+
<Invoice>
4+
<InvoiceNumber>12345</InvoiceNumber>
5+
<Date>2025-04-28</Date>
6+
<CustomerName>John Doe</CustomerName>
7+
8+
9+
10+
<Products>
11+
<Position>1</Position>
12+
<ProductSku>SKU123</ProductSku>
13+
<ProductName>Product A</ProductName>
14+
<Price>100.00</Price>
15+
</Products>
16+
<Products>
17+
<Position>2</Position>
18+
<ProductSku>SKU124</ProductSku>
19+
<ProductName>Product B</ProductName>
20+
<Price>200.00</Price>
21+
</Products>
22+
<Products>
23+
<Position>3</Position>
24+
<ProductSku>SKU125</ProductSku>
25+
<ProductName>Product C</ProductName>
26+
<Price>200.00</Price>
27+
</Products>
28+
<TotalAmount>500.00</TotalAmount>
29+
</Invoice>
30+
<Invoice>
31+
<InvoiceNumber>10295</InvoiceNumber>
32+
<Date>2025-04-20</Date>
33+
<CustomerName>Paul Henriot</CustomerName>
34+
35+
36+
37+
<Products>
38+
<Position>1</Position>
39+
<ProductSku>SKU123</ProductSku>
40+
<ProductName>Product D</ProductName>
41+
<Price>150.00</Price>
42+
</Products>
43+
<Products>
44+
<Position>2</Position>
45+
<ProductSku>SKU124</ProductSku>
46+
<ProductName>Product E</ProductName>
47+
<Price>200.00</Price>
48+
</Products>
49+
<Products>
50+
<Position>3</Position>
51+
<ProductSku>SKU125</ProductSku>
52+
<ProductName>Product F</ProductName>
53+
<Price>220.00</Price>
54+
</Products>
55+
<TotalAmount>570.00</TotalAmount>
56+
</Invoice>
57+
</Invoices>
58+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Generate_invoices_from_xml_data</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
16+
<ItemGroup>
17+
<None Update="Data\InvoiceDetails.xml">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\Template.docx">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Output\.gitkeep">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
// To start each record at new page, so enable this property
14+
document.MailMerge.StartAtNewPage = true;
15+
// Perform the mail merge for the group
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+
static MailMergeDataTable GetRelationalData()
24+
{
25+
//Gets data from XML
26+
Stream xmlStream = File.OpenRead(Path.GetFullPath(@"Data/InvoiceDetails.xml"));
27+
XmlDocument xmlDocument = new XmlDocument();
28+
xmlDocument.Load(xmlStream);
29+
xmlStream.Dispose();
30+
31+
ExpandoObject customerDetails = new ExpandoObject();
32+
GetDataAsExpandoObject((xmlDocument as XmlNode).LastChild, ref customerDetails);
33+
// Treat customerDetails as a dictionary
34+
IDictionary<string, object> customerDict = customerDetails as IDictionary<string, object>;
35+
// Get the "Invoices" list
36+
List<ExpandoObject> invoicesList = customerDict["Invoices"] as List<ExpandoObject>;
37+
// Take the first item in that list
38+
IDictionary<string, object> firstInvoiceGroup = invoicesList[0] as IDictionary<string, object>;
39+
// 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
42+
MailMergeDataTable dataTable = new MailMergeDataTable("Invoices", invoices);
43+
return dataTable;
44+
}
45+
/// <summary>
46+
/// Gets the data as ExpandoObject.
47+
/// </summary>
48+
/// <param name="reader">The reader.</param>
49+
/// <returns></returns>
50+
/// <exception cref="System.Exception">reader</exception>
51+
/// <exception cref="XmlException">Unexpected xml tag + reader.LocalName</exception>
52+
private static void GetDataAsExpandoObject(XmlNode node, ref ExpandoObject dynamicObject)
53+
{
54+
if (node.InnerText == node.InnerXml)
55+
dynamicObject.TryAdd(node.LocalName, node.InnerText);
56+
else
57+
{
58+
List<ExpandoObject> childObjects;
59+
if ((dynamicObject as IDictionary<string, object>).ContainsKey(node.LocalName))
60+
childObjects = (dynamicObject as IDictionary<string, object>)[node.LocalName] as List<ExpandoObject>;
61+
else
62+
{
63+
childObjects = new List<ExpandoObject>();
64+
dynamicObject.TryAdd(node.LocalName, childObjects);
65+
}
66+
ExpandoObject childObject = new ExpandoObject();
67+
foreach (XmlNode childNode in (node as XmlNode).ChildNodes)
68+
{
69+
GetDataAsExpandoObject(childNode, ref childObject);
70+
}
71+
childObjects.Add(childObject);
72+
}
73+
}
74+
#endregion
75+
}
76+
}
77+

0 commit comments

Comments
 (0)