-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomXmlPartActions.vb
66 lines (60 loc) · 3.74 KB
/
CustomXmlPartActions.vb
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
Imports DevExpress.Spreadsheet
Imports System.Xml
Namespace SpreadsheetControl_API_Part03.CodeExamples
Friend Class CustomXmlPartActions
Private Shared Sub StoreCustomXmlPart(ByVal workbook As DevExpress.Spreadsheet.IWorkbook)
#Region "#StoreCustomXmlPart"
workbook.Worksheets(CInt((0))).Cells(CStr(("A1"))).Value = "Custom Xml Test"
' Add an empty custom XML part.
Dim part As DevExpress.Spreadsheet.ICustomXmlPart = workbook.CustomXmlParts.Add()
Dim elem As System.Xml.XmlElement = part.CustomXmlPartDocument.CreateElement("Person")
elem.InnerText = "Stephen Edwards"
part.CustomXmlPartDocument.AppendChild(elem)
' Add an XML part created from string.
Dim xmlString As String = "<?xml version=""1.0"" encoding=""UTF-8""?>
<whitepaper>
<contact>
<firstname>Roger</firstname>
<lastname>Edwards</lastname>
<phone>832-433-0025</phone>
<address>1657 Wines Lane Houston, TX 77099</address>
</contact>
<date>2016-05-18</date>
</whitepaper>"
workbook.CustomXmlParts.Add(xmlString)
' Add an XML part loaded from a file.
Dim xmlDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
xmlDoc.Load("Documents\fishes.xml")
workbook.CustomXmlParts.Add(xmlDoc)
workbook.SaveDocument("Documents\CustomXmlTest.xlsx")
System.IO.File.Copy("Documents\CustomXmlTest.xlsx", "Documents\CustomXmlTest.xlsx.zip", True)
System.Diagnostics.Process.Start("Documents\CustomXmlTest.xlsx.zip")
#End Region ' #StoreCustomXmlPart
End Sub
Private Shared Sub ObtainCustomXmlPart(ByVal workbook As DevExpress.Spreadsheet.IWorkbook)
#Region "#ObtainCustomXmlPart"
workbook.LoadDocument("Documents\CustomXml.xlsx")
Dim xmlDoc As System.Xml.XmlDocument = workbook.CustomXmlParts(CInt((0))).CustomXmlPartDocument
Dim nsmgr As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
Dim xPathString As String = "//Fish[Category='Cod']/ScientificClassification/Reference"
Dim xmlNode As System.Xml.XmlNode = xmlDoc.DocumentElement.SelectSingleNode(xPathString, nsmgr)
Dim hLink As String = xmlNode.InnerText
workbook.Worksheets(CInt((0))).Hyperlinks.Add(workbook.Worksheets(CInt((0))).Cells("A2"), hLink, True)
#End Region ' #ObtainCustomXmlPart
End Sub
Private Shared Sub ModifyCustomXmlPart(ByVal workbook As DevExpress.Spreadsheet.IWorkbook)
#Region "#ModifyCustomXmlPart"
workbook.LoadDocument("Documents\CustomXml.xlsx")
Dim xmlDoc As System.Xml.XmlDocument = workbook.CustomXmlParts(CInt((1))).CustomXmlPartDocument
Dim nsmgr As System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager(xmlDoc.NameTable)
Dim xPathString As String = "//whitepaper/contact[firstname='Roger']/firstname"
Dim xmlNodes As System.Xml.XmlNodeList = xmlDoc.DocumentElement.SelectNodes(xPathString, nsmgr)
For Each node As System.Xml.XmlNode In xmlNodes
node.InnerText = "Stephen"
Next
workbook.SaveDocument("Documents\CustomXmlRogerStephen.xlsx")
workbook.Worksheets(CInt((0))).Cells(CStr(("A2"))).Value = xmlDoc.FirstChild.FirstChild.FirstChild.InnerText
#End Region ' #ModifyCustomXmlPart
End Sub
End Class
End Namespace