I am trying to store some data in an Excel file using it's CustomXMLParts component. I am also failing quite hard.
I have the following code:
Public Shared Sub AddSettingsXMLToDocument()
Dim xmlPart As XElement =
<SoftwareName xmlns="myNamespace">
<Settings>
<FormVersion></FormVersion>
<FormPassword>"Password"</FormPassword>
<DatabaseRequiresAdminMode></DatabaseRequiresAdminMode>
</Settings>
</SoftwareName>
Dim aWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
aWorkbook.CustomXMLParts.Add(xmlPart.ToString(), System.Type.Missing)
End Sub
Public Shared Function GetSettingsXMLFromDocument() As String
Dim aWorkbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
'GET THE ENTIRE THING
Dim retrievedXMLParts As Office.CustomXMLParts = aWorkbook.CustomXMLParts.SelectByNamespace("myNamespace")
Dim customXMLPart As Office.CustomXMLPart = retrievedXMLParts2.Cast(Of Office.CustomXMLPart)().FirstOrDefault()
'TRYING TO GET A SPECIFIC FIELD
Dim formField As Office.CustomXMLNode = customXMLPart2.SelectSingleNode("XpathExpression")
If (Not IsNothing(formField)) Then _
MsgBox("FINALLY GOT MY NODE! " & vbNewLine & d1.XML)
If (IsNothing(formField)) Then _
MsgBox("ARG")
Return FormField.XML
End Function
The line I'm interested in is: Dim formField As Office.CustomXMLNode = customXMLPart2.SelectSingleNode("XpathExpression")
I have pretty tried EVERY thing as a Xpath Expression and I received a null value for everything except "*/".
I also read an article about namespaces, but it won't work with VSTO as Office.CustomXMLParts won't allow you to overload the function with a NamespaceManager parameter. SelectSingleNode returning null for known good xml node path using XPath
How can I build my data out of Office.CustomXMLParts ? How would I update the Workbook.CustomXMLParts ?
Many thanks!
Edit: I am leaving for a few days. I appreciate your input a lot! Unfortunately, I won't be able to confirm any solution until my return.