-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomXmlActions.cs
94 lines (85 loc) · 4.29 KB
/
CustomXmlActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using DevExpress.XtraRichEdit;
using System.Xml;
using DevExpress.XtraRichEdit.API.Native;
namespace DXRichEditControlAPISample.CodeExamples
{
class CustomXmlParts
{
static void AddCustomXmlPart(Document document)
{
#region #AddCustomXmlPart
document.AppendText("This document contains custom XML parts.");
// Add an empty custom XML part.
ICustomXmlPart xmlItem = document.CustomXmlParts.Add();
// Populate the XML part with content.
XmlElement elem = xmlItem.CustomXmlPartDocument.CreateElement("Employees");
elem.InnerText = "Stephen Edwards";
xmlItem.CustomXmlPartDocument.AppendChild(elem);
// Use a string to specify the content for a custom XML part.
string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Employees>
<FirstName>Stephen</FirstName>
<LastName>Edwards</LastName>
<Address>4726 - 11th Ave. N.E.</Address>
<City>Seattle</City>
<Region>WA</Region>
<PostalCode>98122</PostalCode>
<Country>USA</Country>
</Employees>";
document.CustomXmlParts.Insert(1, xmlString);
// Add a custom XML part from a file.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Documents\\Employees.xml");
document.CustomXmlParts.Add(xmlDoc);
document.SaveDocument("Result.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "Result.docx");
#endregion #AddCustomXmlPart
}
static void AccessCustomXmlPart(Document document)
{
#region #AccessCustomXmlPart
// Load a document.
document.LoadDocument("Documents\\CustomXmlParts.docx");
// Access a custom XML file stored in the document.
XmlDocument xmlDoc = document.CustomXmlParts[0].CustomXmlPartDocument;
// Retrieve employee names from the XML file and display them in the document.
XmlNodeList nameList = xmlDoc.GetElementsByTagName("Name");
document.AppendText("Employee list:");
foreach (XmlNode name in nameList)
{
document.AppendText("\r\n \u00B7 " + name.InnerText);
}
document.SaveDocument("Result.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "Result.docx");
#endregion #AccessCustomXmlPart
}
static void RemoveCustomXmlPart(Document document)
{
#region #RemoveCustomXmlPart
document.AppendText("This document contains custom XML parts.");
// Add the first custom XML part.
string xmlString1 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Employees>
<FirstName>Stephen</FirstName>
<LastName>Edwards</LastName>
</Employees>";
var xmlItem1 = document.CustomXmlParts.Add(xmlString1);
// Add the second custom XML part.
string xmlString2 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Employees>
<FirstName>Andrew</FirstName>
<LastName>Fuller</LastName>
</Employees>";
var xmlItem2 = document.CustomXmlParts.Add(xmlString2);
// Remove the first item from the collection.
document.CustomXmlParts.Remove(xmlItem1);
// Use the RemoveAt method to remove an item at the specified position from the collection.
// document.CustomXmlParts.RemoveAt(0);
// Use the Clear method to remove all items from the collection.
// document.CustomXmlParts.Clear();
document.SaveDocument("Result.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "Result.docx");
#endregion #RemoveCustomXmlPart
}
}
}