我正在尝试通过一个唯一属性选择一个节点,但是我没有收到任何回复给我的信息 我已在此处查看:SelectSingleNode returns nothing此处:why does Msxml DocumentElement/SelectSingleNode returns nothing?
我的目标是能够更新其中一个节点属性 我的XML看起来像这样:
<?xml version="1.0"?>
<settings>
<options>
... other nodes here ...
</options>
<selected>
<pref set_number="1" set_name="Five" name="Number" value="5"/>
<pref set_number="3" set_name="Nine" name="Number" value="9"/>
</selected>
</settings>
这是我的Sub (我评论过我尝试过的事情)
Sub WriteXMLAttributeValues()
Dim oDoc As New MSXML2.DOMDocument
Dim fSuccess As Boolean = False
Dim oRoot As MSXML2.IXMLDOMNode = Nothing
Dim oAtrb As MSXML2.IXMLDOMElement = Nothing
Dim oLst As MSXML2.IXMLDOMNodeList = Nothing
oDoc.async = False
oDoc.validateOnParse = False
fSuccess = oDoc.load("C:\settings.xml")
'oRoot = oDoc.childNodes("settings").childNodes("selected").attributes("set_number")
'oRoot = oDoc.selectSingleNode("//settings/selected[@set_number='1' and @value='5']")
'oRoot = oDoc.selectSingleNode("//settings/selected[@set_number='1']")
'oAtrb.setAttribute("name", "Twenty")
'oLst = oDoc.selectSingleNode("/settings/selected[@set_number='1']")
'oAtrb.setAttribute("name", "Twenty")
oRoot = oDoc.getElementsByTagName("set_number")
oDoc.save("C:\settings2.xml")
End Sub
我正在尝试更改第一个&lt; pref&gt; 的属性从“Five”到“Twenty”
我想知道我做错了什么,所以我可以正确更新属性。
答案 0 :(得分:2)
忘记路径中的pref元素。看起来应该是这样的。
Dim node as XmlNode
node = oDoc.selectSingleNode("//settings/selected/pref[@set_number='1']")
然后您可以更改属性:
node.Attributes("set_name").Value = "Twenty"