powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Добавить атрибут в узел XML-ки
1 сообщений из 1, страница 1 из 1
Добавить атрибут в узел XML-ки
    #35928898
Есть XML приблизительного вида:
<bom>
<item>
<number>1</number>
<quote>3</quote>
</item>
<item>
<number>2</number>
<quote>32</quote>
</item>
<item>
<number>3</number>
<quote>123</quote>
</item>
</bom>

То есть, узлы item - одинакового имени.
Для корректного распихивания по таблицам мне необходимо привести ее к виду:


<bom>
<item id="1">
<number>1</number>
<quote>3</quote>
</item>
<item id="2">
<number>2</number>
<quote>32</quote>
</item>
<item id = "3">
<number>3</number>
<quote>123</quote>
</item>
</bom>

То есть, к узлу item добавить атрибут id.

Никак не получается. Делаю так:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
Private Sub Command2_Click()
Dim att As IXMLDOMAttribute
Dim aElement As IXMLDOMElement, itemcounter as Integer
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.Load XMLFilePath
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set objListOfNodes = xmlDoc.selectNodes("//bom/*")
itemcounter =  1 
For Each objNode In objListOfNodes
        If objNode.nodeName = "item" Then

'-------attributes---
'---create an attribute using the createAttribute() method
'---at the same time set its name
    Set aElement = xmlDoc.documentElement.selectSingleNode("item")
    Set att = xmlDoc.createAttribute("id")
'---set the attributes 'text' property
    att.Text = itemcounter
'--for the aElement, which is 'student' set a named
'---attribute, it's name is att
aElement.Attributes.setNamedItem att
Debug.Print xmlDoc.documentElement.selectSingleNode("item")
itemcounter = itemcounter+ 1 
        End If
Next
xmlDoc.save (App.Path & "\out.xml")
End Sub

Вылетает на строке

aElement.Attributes.setNamedItem att

ПАМАГИТЕ!!!
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / Visual Basic [игнор отключен] [закрыт для гостей] / Добавить атрибут в узел XML-ки
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]