使用 VBA 迭代 XML 节点

发布于 2024-10-10 02:51:56 字数 1920 浏览 4 评论 0原文

注意:- 这可能只是一个使用 VBA 迭代 XML 节点的问题。请看这个问题的底部。如果我们可以在不使用 MSXML2.DOMDocument 的情况下进行迭代,那就太好了

我看到 this< /a> 问题回答了我关于如何检索 CustomXMLPart 的部分问题。但是,我无法遍历 Xml。这样,这可能不是特定于 CustomXmlPart,它可能只是一个使用 VBA 迭代 XML 的问题。以下是我的 CustomXMLPart 中的 XML。

<Items>
<Item1>Item1</Item1>
<Item2>Item2</Item2>
<Item3>Item3</Item3>
</Items>

这就是我将上述 XML 添加为 CustomXmlPart 的方式:-

static void AddCustomTableXmlPart(WordprocessingDocument document)
        {
            MainDocumentPart mainDocumentPart = document.MainDocumentPart;
            XDocument itemXml = GetItemsAsCustomXML();

            if (mainDocumentPart.GetPartsCountOfType<CustomXmlPart>() > 0)
                mainDocumentPart.DeleteParts<CustomXmlPart>(mainDocumentPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream()))
            {
                ts.Write(itemXml.ToString());
                ts.Flush();
            }
        }

这就是我在宏中访问它的方式:-

Dim itemNode As xmlNode
Dim itemChildren As XMLNodes

' 以下行抛出运行时错误 '运行时错误 '13' - '类型不匹配' 不知道为什么。

**Set itemChildren= ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes**

有趣的是,当我快速观看 ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes 时,我在快速观看窗口中看到子项目。 itemChildren 变量的赋值是否不正确?

我想遍历所有项目并获取所有项目的文本。有人可以帮忙吗?

Note:- It just might be a iterating XML nodes using VBA question. Please look at the bottom of this question. It would be good If we can iterate without using MSXML2.DOMDocument

I see the this question which answers part of my question on how to retrieve the CustomXMLPart. However, I am not able to iterate through the Xml. That way, this might not be specific to CustomXmlPart, It just might be a iterating XML using VBA question. Following is the XML I have in my CustomXMLPart.

<Items>
<Item1>Item1</Item1>
<Item2>Item2</Item2>
<Item3>Item3</Item3>
</Items>

This is how I add the above XML as CustomXmlPart:-

static void AddCustomTableXmlPart(WordprocessingDocument document)
        {
            MainDocumentPart mainDocumentPart = document.MainDocumentPart;
            XDocument itemXml = GetItemsAsCustomXML();

            if (mainDocumentPart.GetPartsCountOfType<CustomXmlPart>() > 0)
                mainDocumentPart.DeleteParts<CustomXmlPart>(mainDocumentPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream()))
            {
                ts.Write(itemXml.ToString());
                ts.Flush();
            }
        }

and this is how I am accessing it in the macro:-

Dim itemNode As xmlNode
Dim itemChildren As XMLNodes

' The below line throws a run-time error 'Run-time error '13' - 'type mismatch ' not sure why.

**Set itemChildren= ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes**

Interestingly, when I quick watch ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectSingleNode("//Items").ChildNodes, I see child items in the quick watch window. Is the assignment to the itemChildren variable incorrect?

I want to iterate through all the items and get get text for all of them. Could anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-10-17 02:51:56

好的,我就是这样做的。发布以防万一它对某人有用。显然,您不需要使用“CustomXMLNodes”对象。这仍然可以使用 SelectNodes 来完成。下面的函数表明了这一点。另外,我在将项目添加到列表时没有检查空字符串。

Sub LoadItems()
     Dim totalItemsCount As Integer
     totalItemsCount = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count
     Dim item As String

     For i = 1 To totalItemsCount
        item = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes(i).text
        item = Replace(item, " ", Empty)

        If Len(item) > 1 Then
        ItemUserControl.lstItems.AddItem pvargItem:item
        End If
     Next i
End Sub

Ok, this is how I did it. Posting just in case it is useful for somebody. Apparently, you need not use "CustomXMLNodes" object. This can still be done using SelectNodes. The below function shows that. Also, I was not checking for empty string while adding item to the list.

Sub LoadItems()
     Dim totalItemsCount As Integer
     totalItemsCount = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count
     Dim item As String

     For i = 1 To totalItemsCount
        item = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes(i).text
        item = Replace(item, " ", Empty)

        If Len(item) > 1 Then
        ItemUserControl.lstItems.AddItem pvargItem:item
        End If
     Next i
End Sub
断念 2024-10-17 02:51:56

使用:

将 itemChildren 调暗为 CustomXMLNodes

Use:

Dim itemChildren As CustomXMLNodes

口干舌燥 2024-10-17 02:51:56

我认为您收到类型不匹配错误的原因是 ChildNodes 返回 IXMLDOMNodeList 而不是 XMLNodes

尝试使用 Dim itemChildren As IXMLDOMNodeList 来代替。

http://msdn.microsoft.com/en-我们/library/ms757053(v=vs.85).aspx

I think the reason you're getting a type mismatch error is because ChildNodesreturns a IXMLDOMNodeList not XMLNodes.

Try Dim itemChildren As IXMLDOMNodeList instead.

http://msdn.microsoft.com/en-us/library/ms757053(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文