【问题标题】:Retrieving Office.customeXMLparts without namespaces检索没有命名空间的 Office.customXMLparts
【发布时间】:2014-08-24 21:30:48
【问题描述】:

我正在尝试使用它的 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")

我已经尝试了所有的 Xpath 表达式,并且我收到了除“*/”之外的所有内容的空值。

我还阅读了一篇关于命名空间的文章,但它不适用于 VSTO,因为 Office.CustomXMLParts 不允许您使用 NamespaceManager 参数重载该函数。 SelectSingleNode returning null for known good xml node path using XPath

如何从 Office.CustomXMLParts 构建我的数据? 如何更新 Workbook.CustomXMLParts ?

非常感谢!

编辑:我要离开几天。非常感谢您的意见!很遗憾,在我回来之前我无法确认任何解决方案。

【问题讨论】:

    标签: c# xml vb.net excel xpath


    【解决方案1】:

    如果您真的找不到注册命名空间前缀并在 XPath 中使用它的方法,您可以求助于忽略命名空间并通过其本地名称过滤节点。例如(为便于阅读而格式化):

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

    【讨论】:

    • 谢谢!它就像一个魅力!我找不到关于这种语法的任何信息,所以我必须在这件事上做功课。
    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 2011-08-23
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2012-12-31
    • 2013-03-25
    • 1970-01-01
    相关资源
    最近更新 更多