1

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.

Community
  • 1
  • 1
Andre Taron
  • 121
  • 6

1 Answers1

0

If you really can't find a way to register namespace prefix and use it in your XPath, you may be able to resort to ignore the namespace and filter nodes by it's local name. For example (formatted for the sake of readability) :

//*[local-name()='SoftwareName']
   /*[local-name()='Settings']
   /*[local-name()='FormPassword']
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks! It worked like a charm! I couldn't find any information about such a syntax, so I'll have to do my homework on the matter. – Andre Taron Aug 25 '14 at 13:05