2016-03-09 103 views
0

我有一个xml文件,我想从数字标签中取一个文本('aaa')为一个字符串。它看起来像这样:从XML文件中获取文本到一个字符串

<header_xml> <number>aaa</number> </header_xml>

我试图用在网上找到的方法,但他们不工作。

这是我的代码:

Set oXMLFile = CreateObject("Microsoft.XMLDOM") 
sGeneratorPath = ThisWorkbook.Path 

With Application.FileDialog(msoFileDialogOpen) ' --- Open FileDialog 
    .InitialFileName = sGeneratorPath ' --- inital path 
    .Filters.Clear 
    .Filters.Add "XML Files (*.xml)", "*.xml" ' --- add filter to choose only xml files 
    .AllowMultiSelect = False ' --- to select only one file 

    If .Show = False Then ' --- if operation aborted, display message 
     MsgBox "Operation Cancelled" 
     End 
    End If 

    sXMLPath = .SelectedItems(1) ' --- path of xml file 
End With 

oXMLFile.Load (sXMLPath) 

Set HWNode = oXMLFile.SelectSingleNode("/header_xml/number/") 
sHWID = HWNode.Text 
MsgBox sHWID 

MsgBox "Done" 
+1

创建这个进程id - '的CreateObject( “Microsoft.XMLDOM”)' - 现在已经过时。使用'CreateObject(“MSXML2.DOMDocument.6.0”)'代替 - 参见[这里](http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right-version- of-msxml-in-internet-explorer.aspx)获取更多信息 – barrowc

回答

2

你真的很接近。只需删除多余的/number

Set HWNode = oXMLFile.SelectSingleNode("/header_xml/number") 

测试

enter image description here

+0

啊,我正在尝试将近2个小时,但没有看到。非常感谢你! – GohanP

相关问题