英文:
Read an xml document and updating its content into listview by using loop in vb.net
问题
我已将xml文档的第一个节点添加到ListView中,但无法更新所有节点。
我使用了xml文档的XElement属性。
我甚至尝试了序列化方法,但在我的IDE上不起作用。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim doc As XDocument = XDocument.Load("saved_data.xml")
Dim node As XElement = XDocument.Load("saved_data.xml").Root
For Each nodes In node
Dim str(2) As String
Dim itm As ListViewItem
str(0) = node.<data>.<NAME>.Value
str(1) = node.<data>.<PHONE>.Value
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
str(0) = Nothing
str(1) = Nothing
Next
End Sub
英文:
I have pushed the first node of xml document in the listview but not able to update all the nodes
I have used the xelement property of xml document.
I have even used the serialization method but its not working on my ide
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim doc As XDocument = XDocument.Load("saved_data.xml")
Dim node As XElement = XDocument.Load("saved_data.xml").Root
For Each nodes In node
Dim str(2) As String
Dim itm As ListViewItem
str(0) = node.<data>.<NAME>.Value
str(1) = node.<data>.<PHONE>.Value
itm = New ListViewItem(str)
ListView1.Items.Add(itm)
str(0) = Nothing
str(1) = Nothing
Next
end sub
答案1
得分: 1
Dim xe As XElement
'for production
' xe = XElement.Load("saved_data.xml")
'for testing use a literal
xe = <root>
<data>
<NAME>one</NAME>
<PHONE>one ph</PHONE>
</data>
<data>
<NAME>two</NAME>
<PHONE>two ph</PHONE>
</data>
<data>
<NAME>three</NAME>
<PHONE>three ph</PHONE>
</data>
</root>
For Each data As XElement In xe.<data>
ListView1.Items.Add(String.Format("{0} {1}", data.<NAME>.Value, data.<PHONE>.Value))
Next
英文:
I had to guess at what the XML looked like but this should help,
Dim xe As XElement
'for production
' xe = XElement.Load("saved_data.xml")
'for testing use a literal
xe = <root>
<data>
<NAME>one</NAME>
<PHONE>one ph</PHONE>
</data>
<data>
<NAME>two</NAME>
<PHONE>two ph</PHONE>
</data>
<data>
<NAME>three</NAME>
<PHONE>three ph</PHONE>
</data>
</root>
For Each data As XElement In xe.<data>
ListView1.Items.Add(String.Format("{0} {1}", data.<NAME>.Value, data.<PHONE>.Value))
Next
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论