/
CustomXmlActions.cs
113 lines (98 loc) · 4.79 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using DevExpress.XtraRichEdit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using DevExpress.XtraRichEdit.API.Native;
namespace RichEditDocumentServerAPIExample.CodeExamples
{
class CustomXmlActions
{
public static Action<RichEditDocumentServer> AddCustomXmlPartAction = AddCustomXmlPart;
public static Action<RichEditDocumentServer> AccessCustomXmlPartAction = AccessCustomXmlPart;
public static Action<RichEditDocumentServer> RemoveCustomXmlPartAction = RemoveCustomXmlPart;
static void AddCustomXmlPart(RichEditDocumentServer wordProcessor)
{
#region #AddCustomXmlPart
// Access a document.
Document document = wordProcessor.Document;
// Append text to the document.
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);
// Specify the custom XML part content.
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);
#endregion #AddCustomXmlPart
}
static void AccessCustomXmlPart(RichEditDocumentServer wordProcessor)
{
#region #AccessCustomXmlPart
// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
// Access a document.
Document document = wordProcessor.Document;
if (document.CustomXmlParts.Count > 0)
{
// 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);
}
}
#endregion #AccessCustomXmlPart
}
static void RemoveCustomXmlPart(RichEditDocumentServer wordProcessor)
{
#region #RemoveCustomXmlPart
// Access a document.
Document document = wordProcessor.Document;
// Append text to the document.
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();
#endregion #RemoveCustomXmlPart
}
}
}