读取 XML 属性 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5297068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read XML Attribute VBA
提问by Christoph Wolf
I am trying to get the attribute of a single node in VBA, but can't manage it using DOM
我试图在 VBA 中获取单个节点的属性,但无法使用 DOM 管理它
The XML looks like following:
XML 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetUserInfoResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<GetUserInfoResult>
<GetUserInfo>
<User ID="16" Name="" LoginName="login" Email="" Notes="" IsSiteAdmin="False" IsDomainGroup="False" />
</GetUserInfo>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body>
</soap:Envelope>
I am basically just trying to get the value of the ID attribute. Any help would be appreciated.
我基本上只是想获取 ID 属性的值。任何帮助,将不胜感激。
回答by Jon Egerton
Try:
尝试:
(Include a reference to Microsoft XML v3, I saved your xml to a file on my desktop)
(包括对 Microsoft XML v3 的引用,我将您的 xml 保存到桌面上的文件中)
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.Load ("C:\users\jon\desktop\test.xml")
Dim id As String
id = xmlDoc.SelectSingleNode("//GetUserInfo/User").Attributes.getNamedItem("ID").Text
回答by thanassis
I tried using similar code to load and extract attributes from a web service provided XML file. It turns out that unless you set the xDoc.async property to false, xDoc.Load() returns immediately and then the rest of your code goes to waste.
我尝试使用类似的代码从 Web 服务提供的 XML 文件加载和提取属性。事实证明,除非您将 xDoc.async 属性设置为 false,否则 xDoc.Load() 会立即返回,然后您的其余代码就会浪费掉。

