使用 VBA 迭代 XML 节点
注意:- 这可能只是一个使用 VBA 迭代 XML 节点的问题。请看这个问题的底部。如果我们可以在不使用 MSXML2.DOMDocument 的情况下进行迭代,那就太好了
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我就是这样做的。发布以防万一它对某人有用。显然,您不需要使用“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.
使用:
Use:
我认为您收到类型不匹配错误的原因是
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
ChildNodes
returns aIXMLDOMNodeList
notXMLNodes
.Try
Dim itemChildren As IXMLDOMNodeList
instead.http://msdn.microsoft.com/en-us/library/ms757053(v=vs.85).aspx