检索没有名称空间的Office.customeXMLparts

我正在尝试使用它的CustomXMLParts组件在Excel文件中存储一些数据。 我也很辛苦

我有以下代码:

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 

我感兴趣的行是: Dim formField As Office.CustomXMLNode = customXMLPart2.SelectSingleNode(“XpathExpression”)

我已经相当多的东西作为一个Xpathexpression式尝试,除了“* /”我收到一个空值。

我也阅读了一篇关于命名空间的文章,但是它不能和VSTO一起工作.CustomXMLParts不允许你用NamespaceManager参数重载这个函数。 SelectSingleNode使用XPath为已知良好的xml节点path返回null

如何从Office.CustomXMLParts构build数据? 我将如何更新Workbook.CustomXMLParts?

非常感谢!

编辑:我要离开几天。 我非常感谢你的投入! 不幸的是,在我返回之前,我将无法确认任何解决scheme。

如果你真的找不到注册命名空间前缀的方法,并在你的XPath中使用它,你可能会忽略命名空间和filter节点的本地名称。 例如(格式化为了可读性):

 //*[local-name()='SoftwareName'] /*[local-name()='Settings'] /*[local-name()='FormPassword']