使用 VBA 迭代 XML 节点 [英] Iterating XML nodes using VBA

查看:54
本文介绍了使用 VBA 迭代 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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>

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

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

' 下面这行抛出一个运行时错误 'Run-time error '13' - 'type mismatch' 不知道为什么.

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

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

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?

推荐答案

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

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

这篇关于使用 VBA 迭代 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆