Skip to content

Commit f221bd9

Browse files
authored
Merge pull request #95 from aspose-cells/v3/xml-maps
[AUTO] XML Maps Examples Update
2 parents fa2e9b8 + 73d73f0 commit f221bd9

65 files changed

Lines changed: 4296 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

index.json

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20774,6 +20774,161 @@
2077420774
"file": "import-an-xml-map-directly-from-an-xml-file-into-a-workbook-with-workbookimportxml.cs",
2077520775
"title": "Import an XML map directly from an XML file into a workbook with Workbook.ImportXml."
2077620776
},
20777+
{
20778+
"category": "xml-maps",
20779+
"file": "add-a-comment-to-each-cell-that-is-linked-to-an-xml-element-displaying-the-elements-xpath.cs",
20780+
"title": "Add a comment to each cell that is linked to an XML element, displaying the element's XPath."
20781+
},
20782+
{
20783+
"category": "xml-maps",
20784+
"file": "add-a-new-xml-map-to-a-workbook-that-already-contains-multiple-maps-and-manage-their-order.cs",
20785+
"title": "Add a new XML map to a workbook that already contains multiple maps and manage their order."
20786+
},
20787+
{
20788+
"category": "xml-maps",
20789+
"file": "after-importing-xml-recalculate-all-formulas-to-ensure-dependent-calculations-reflect-the-new-data.cs",
20790+
"title": "After importing XML, recalculate all formulas to ensure dependent calculations reflect the new data."
20791+
},
20792+
{
20793+
"category": "xml-maps",
20794+
"file": "apply-a-custom-style-to-cells-mapped-to-the-customername-element-to-highlight-customer-names.cs",
20795+
"title": "Apply a custom style to cells mapped to the /Customer/Name element to highlight customer names."
20796+
},
20797+
{
20798+
"category": "xml-maps",
20799+
"file": "apply-data-validation-rules-to-mapped-cells-to-ensure-imported-xml-values-meet-business-constraints.cs",
20800+
"title": "Apply data validation rules to mapped cells to ensure imported XML values meet business constraints."
20801+
},
20802+
{
20803+
"category": "xml-maps",
20804+
"file": "batch-export-xml-data-from-multiple-workbooks-by-iterating-files-and-invoking-exportxml-for-each-map.cs",
20805+
"title": "Batch export XML data from multiple workbooks by iterating files and invoking ExportXml for each map."
20806+
},
20807+
{
20808+
"category": "xml-maps",
20809+
"file": "batch-process-a-folder-of-workbooks-adding-the-same-xml-map-to-each-file-programmatically.cs",
20810+
"title": "Batch process a folder of workbooks, adding the same XML map to each file programmatically."
20811+
},
20812+
{
20813+
"category": "xml-maps",
20814+
"file": "compare-two-xml-maps-within-the-same-workbook-to-identify-overlapping-cell-assignments-and-resolve-conflicts.cs",
20815+
"title": "Compare two XML maps within the same workbook to identify overlapping cell assignments and resolve conflicts."
20816+
},
20817+
{
20818+
"category": "xml-maps",
20819+
"file": "configure-the-workbook-to-automatically-refresh-xml-map-data-when-the-source-xml-file-changes-on-disk.cs",
20820+
"title": "Configure the workbook to automatically refresh XML map data when the source XML file changes on disk."
20821+
},
20822+
{
20823+
"category": "xml-maps",
20824+
"file": "configure-the-xml-map-to-ignore-whitespace-nodes-during-import-to-prevent-unwanted-blank-entries.cs",
20825+
"title": "Configure the XML map to ignore whitespace nodes during import to prevent unwanted blank entries."
20826+
},
20827+
{
20828+
"category": "xml-maps",
20829+
"file": "create-a-backup-copy-of-the-workbook-before-modifying-xml-maps-to-allow-easy-rollback-if-needed.cs",
20830+
"title": "Create a backup copy of the workbook before modifying XML maps to allow easy rollback if needed."
20831+
},
20832+
{
20833+
"category": "xml-maps",
20834+
"file": "create-a-custom-function-that-transforms-xml-node-values-before-they-are-written-to-mapped-cells.cs",
20835+
"title": "Create a custom function that transforms XML node values before they are written to mapped cells."
20836+
},
20837+
{
20838+
"category": "xml-maps",
20839+
"file": "create-a-new-workbook-add-a-worksheet-and-define-an-xml-map-using-a-xsd-file.cs",
20840+
"title": "Create a new workbook, add a worksheet, and define an XML map using a XSD file."
20841+
},
20842+
{
20843+
"category": "xml-maps",
20844+
"file": "create-a-pivot-table-that-summarizes-data-from-cells-mapped-to-the-ordersorder-element.cs",
20845+
"title": "Create a pivot table that summarizes data from cells mapped to the /Orders/Order element."
20846+
},
20847+
{
20848+
"category": "xml-maps",
20849+
"file": "create-a-scheduled-task-that-refreshes-all-xml-maps-in-a-set-of-workbooks-every-night-at-midnight.cs",
20850+
"title": "Create a scheduled task that refreshes all XML maps in a set of workbooks every night at midnight."
20851+
},
20852+
{
20853+
"category": "xml-maps",
20854+
"file": "enumerate-the-xmlmapcollection-and-log-each-maps-name-and-root-element-name-for-debugging.cs",
20855+
"title": "Enumerate the XmlMapCollection and log each map\u2019s name and root element name for debugging."
20856+
},
20857+
{
20858+
"category": "xml-maps",
20859+
"file": "export-mapped-data-to-a-json-file-by-converting-the-xml-nodes-to-equivalent-json-structures.cs",
20860+
"title": "Export mapped data to a JSON file by converting the XML nodes to equivalent JSON structures."
20861+
},
20862+
{
20863+
"category": "xml-maps",
20864+
"file": "export-mapped-xml-data-as-a-compressed-zip-archive-containing-separate-files-for-each-map.cs",
20865+
"title": "Export mapped XML data as a compressed .zip archive containing separate files for each map."
20866+
},
20867+
{
20868+
"category": "xml-maps",
20869+
"file": "export-the-current-xml-map-data-to-a-separate-xml-file-preserving-original-schema-structure.cs",
20870+
"title": "Export the current XML map data to a separate .xml file preserving original schema structure."
20871+
},
20872+
{
20873+
"category": "xml-maps",
20874+
"file": "export-the-workbook-to-an-ods-file-while-ensuring-that-xml-map-definitions-are-retained-in-the-output.cs",
20875+
"title": "Export the workbook to an ODS file while ensuring that XML map definitions are retained in the output."
20876+
},
20877+
{
20878+
"category": "xml-maps",
20879+
"file": "export-the-workbook-to-pdf-while-preserving-the-visual-representation-of-mapped-cells-and-their-data.cs",
20880+
"title": "Export the workbook to PDF while preserving the visual representation of mapped cells and their data."
20881+
},
20882+
{
20883+
"category": "xml-maps",
20884+
"file": "export-xml-data-for-a-specific-map-to-a-file-using-workbookexportxml-with-map-index.cs",
20885+
"title": "Export XML data for a specific map to a file using Workbook.ExportXml with map index."
20886+
},
20887+
{
20888+
"category": "xml-maps",
20889+
"file": "export-xml-data-for-a-specific-map-to-a-memory-stream-using-workbookexportxml-overload.cs",
20890+
"title": "Export XML data for a specific map to a memory stream using Workbook.ExportXml overload."
20891+
},
20892+
{
20893+
"category": "xml-maps",
20894+
"file": "export-xml-with-indentation-enabled-by-configuring-exportxmloptions-before-calling-exportxml.cs",
20895+
"title": "Export XML with indentation enabled by configuring ExportXmlOptions before calling ExportXml."
20896+
},
20897+
{
20898+
"category": "xml-maps",
20899+
"file": "export-xml-with-utf8-encoding-by-specifying-encodingutf8-parameter-in-exportxml-method.cs",
20900+
"title": "Export XML with UTF\u20118 encoding by specifying Encoding.UTF8 parameter in ExportXml method."
20901+
},
20902+
{
20903+
"category": "xml-maps",
20904+
"file": "export-xml-without-xml-declaration-by-setting-exportxmloptionsomitxmldeclaration-to-true.cs",
20905+
"title": "Export XML without XML declaration by setting ExportXmlOptions.OmitXmlDeclaration to true."
20906+
},
20907+
{
20908+
"category": "xml-maps",
20909+
"file": "generate-a-summary-report-listing-each-xml-map-its-root-element-and-the-number-of-linked-cells.cs",
20910+
"title": "Generate a summary report listing each XML map, its root element, and the number of linked cells."
20911+
},
20912+
{
20913+
"category": "xml-maps",
20914+
"file": "implement-a-routine-that-clears-all-cell-values-linked-to-a-specific-xml-map-without-removing-the-map.cs",
20915+
"title": "Implement a routine that clears all cell values linked to a specific XML map without removing the map."
20916+
},
20917+
{
20918+
"category": "xml-maps",
20919+
"file": "implement-a-ui-dialog-that-lets-users-select-an-xml-schema-file-to-create-a-new-map.cs",
20920+
"title": "Implement a UI dialog that lets users select an XML schema file to create a new map."
20921+
},
20922+
{
20923+
"category": "xml-maps",
20924+
"file": "implement-error-handling-to-catch-exceptions-when-an-xml-map-path-does-not-exist-in-the-schema.cs",
20925+
"title": "Implement error handling to catch exceptions when an XML map path does not exist in the schema."
20926+
},
20927+
{
20928+
"category": "xml-maps",
20929+
"file": "import-an-xml-map-directly-from-an-xml-file-into-a-workbook-with-workbookimportxml.cs",
20930+
"title": "Import an XML map directly from an XML file into a workbook with Workbook.ImportXml."
20931+
},
2077720932
{
2077820933
"category": "xml-maps",
2077920934
"file": "import-an-xml-map-from-an-xsd-file-into-a-workbook-using-workbookimportxml-method.cs",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Cells;
4+
5+
namespace AsposeCellsXmlCommentDemo
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
try
12+
{
13+
// Create a new workbook
14+
Workbook wb = new Workbook();
15+
16+
// Sample XML content
17+
string xml = @"<Transmittals><Issued_Document>Doc1</Issued_Document><Issued_Document>Doc2</Issued_Document></Transmittals>";
18+
19+
// Write XML to a temporary file (required by Aspose.Cells XmlMaps.Add)
20+
string tempXmlPath = Path.Combine(Path.GetTempPath(), "TempXmlMap.xml");
21+
File.WriteAllText(tempXmlPath, xml);
22+
23+
// Add the XML map from the temporary file
24+
int mapIndex = wb.Worksheets.XmlMaps.Add(tempXmlPath);
25+
XmlMap xmlMap = wb.Worksheets.XmlMaps[mapIndex];
26+
xmlMap.Name = "Transmittals_Map";
27+
28+
// Clean up the temporary file
29+
if (File.Exists(tempXmlPath))
30+
{
31+
File.Delete(tempXmlPath);
32+
}
33+
34+
// Get the first worksheet
35+
Worksheet sheet = wb.Worksheets[0];
36+
Cells cells = sheet.Cells;
37+
38+
// Define cells to link with XML elements
39+
var linkedCells = new[]
40+
{
41+
new { Row = 0, Column = 0, Path = "/Transmittals/Issued_Document[1]" },
42+
new { Row = 1, Column = 0, Path = "/Transmittals/Issued_Document[2]" }
43+
};
44+
45+
// Link cells and add visible comments showing the XPath
46+
foreach (var item in linkedCells)
47+
{
48+
cells.LinkToXmlMap(xmlMap.Name, item.Row, item.Column, item.Path);
49+
50+
int commentIdx = sheet.Comments.Add(item.Row, item.Column);
51+
Comment comment = sheet.Comments[commentIdx];
52+
comment.Note = $"XPath: {item.Path}";
53+
comment.IsVisible = true;
54+
}
55+
56+
// Save the workbook
57+
string outputPath = "XmlLinkedComments.xlsx";
58+
wb.Save(outputPath);
59+
Console.WriteLine($"Workbook saved successfully to '{outputPath}'.");
60+
}
61+
catch (Exception ex)
62+
{
63+
Console.WriteLine($"Error: {ex.Message}");
64+
}
65+
}
66+
}
67+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Cells;
4+
5+
namespace AsposeCellsXmlMapOrderDemo
6+
{
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
try
12+
{
13+
// Create a new workbook
14+
Workbook workbook = new Workbook();
15+
16+
// -----------------------------------------------------------------
17+
// Prepare temporary XSD files for the XML maps
18+
// -----------------------------------------------------------------
19+
string tempDir = Path.Combine(Path.GetTempPath(), "AsposeXmlMaps");
20+
Directory.CreateDirectory(tempDir);
21+
22+
string schema1Path = Path.Combine(tempDir, "schema1.xsd");
23+
string schema2Path = Path.Combine(tempDir, "schema2.xsd");
24+
string schemaNewPath = Path.Combine(tempDir, "schemaNew.xsd");
25+
26+
string schema1 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
27+
<xs:element name='Root1'>
28+
<xs:complexType>
29+
<xs:sequence>
30+
<xs:element name='Item1' type='xs:string'/>
31+
</xs:sequence>
32+
</xs:complexType>
33+
</xs:element>
34+
</xs:schema>";
35+
string schema2 = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
36+
<xs:element name='Root2'>
37+
<xs:complexType>
38+
<xs:sequence>
39+
<xs:element name='Item2' type='xs:string'/>
40+
</xs:sequence>
41+
</xs:complexType>
42+
</xs:element>
43+
</xs:schema>";
44+
string schemaNew = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
45+
<xs:element name='RootNew'>
46+
<xs:complexType>
47+
<xs:sequence>
48+
<xs:element name='ItemNew' type='xs:string'/>
49+
</xs:sequence>
50+
</xs:complexType>
51+
</xs:element>
52+
</xs:schema>";
53+
54+
File.WriteAllText(schema1Path, schema1);
55+
File.WriteAllText(schema2Path, schema2);
56+
File.WriteAllText(schemaNewPath, schemaNew);
57+
58+
// -----------------------------------------------------------------
59+
// Add initial XML maps (simulating a workbook that already has maps)
60+
// -----------------------------------------------------------------
61+
if (File.Exists(schema1Path))
62+
{
63+
int index1 = workbook.Worksheets.XmlMaps.Add(schema1Path);
64+
workbook.Worksheets.XmlMaps[index1].Name = "FirstMap";
65+
}
66+
67+
if (File.Exists(schema2Path))
68+
{
69+
int index2 = workbook.Worksheets.XmlMaps.Add(schema2Path);
70+
workbook.Worksheets.XmlMaps[index2].Name = "SecondMap";
71+
}
72+
73+
// ---------------------------------------------------------------
74+
// Add a new XML map that should appear between the existing maps
75+
// ---------------------------------------------------------------
76+
int newIndex = -1;
77+
if (File.Exists(schemaNewPath))
78+
{
79+
newIndex = workbook.Worksheets.XmlMaps.Add(schemaNewPath);
80+
workbook.Worksheets.XmlMaps[newIndex].Name = "InsertedMap";
81+
}
82+
83+
// ---------------------------------------------------------------
84+
// Reorder: move the newly added map to position 1 (second place)
85+
// ---------------------------------------------------------------
86+
if (newIndex >= 0)
87+
{
88+
// Remove the map from its current position
89+
workbook.Worksheets.XmlMaps.RemoveAt(newIndex);
90+
91+
// Store existing maps temporarily
92+
XmlMap[] existingMaps = new XmlMap[workbook.Worksheets.XmlMaps.Count];
93+
for (int i = 0; i < existingMaps.Length; i++)
94+
existingMaps[i] = workbook.Worksheets.XmlMaps[i];
95+
96+
// Clear the collection
97+
workbook.Worksheets.XmlMaps.Clear();
98+
99+
// Re‑add maps in the required order:
100+
// 1. FirstMap
101+
// 2. InsertedMap (new map)
102+
// 3. SecondMap
103+
if (File.Exists(schema1Path))
104+
workbook.Worksheets.XmlMaps.Add(schema1Path);
105+
if (File.Exists(schemaNewPath))
106+
workbook.Worksheets.XmlMaps.Add(schemaNewPath);
107+
if (File.Exists(schema2Path))
108+
workbook.Worksheets.XmlMaps.Add(schema2Path);
109+
110+
// Restore map names (names are lost after Clear/Add)
111+
if (workbook.Worksheets.XmlMaps.Count > 0)
112+
workbook.Worksheets.XmlMaps[0].Name = "FirstMap";
113+
if (workbook.Worksheets.XmlMaps.Count > 1)
114+
workbook.Worksheets.XmlMaps[1].Name = "InsertedMap";
115+
if (workbook.Worksheets.XmlMaps.Count > 2)
116+
workbook.Worksheets.XmlMaps[2].Name = "SecondMap";
117+
}
118+
119+
// ---------------------------------------------------------------
120+
// Display the current order of XML maps
121+
// ---------------------------------------------------------------
122+
Console.WriteLine("Current XML map order:");
123+
for (int i = 0; i < workbook.Worksheets.XmlMaps.Count; i++)
124+
{
125+
Console.WriteLine($"{i}: {workbook.Worksheets.XmlMaps[i].Name}");
126+
}
127+
128+
// Save the workbook (the XML maps are stored inside the file)
129+
string outputPath = Path.Combine(Environment.CurrentDirectory, "WorkbookWithOrderedXmlMaps.xlsx");
130+
workbook.Save(outputPath);
131+
Console.WriteLine($"Workbook saved to: {outputPath}");
132+
}
133+
catch (Exception ex)
134+
{
135+
Console.WriteLine($"Error: {ex.Message}");
136+
}
137+
}
138+
}
139+
}

0 commit comments

Comments
 (0)