读取一个XML文档,并使用VB.NET中的循环将其内容更新到ListView中。

huangapple go评论70阅读模式
英文:

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(&quot;saved_data.xml&quot;)
     Dim node As XElement = XDocument.Load(&quot;saved_data.xml&quot;).Root
     For Each nodes In node
            Dim str(2) As String
            Dim itm As ListViewItem
            str(0) = node.&lt;data&gt;.&lt;NAME&gt;.Value
            str(1) = node.&lt;data&gt;.&lt;PHONE&gt;.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
    &#39;for production
    &#39; xe = XElement.Load(&quot;saved_data.xml&quot;)
    &#39;for testing use a literal

    xe = &lt;root&gt;
             &lt;data&gt;
                 &lt;NAME&gt;one&lt;/NAME&gt;
                 &lt;PHONE&gt;one ph&lt;/PHONE&gt;
             &lt;/data&gt;
             &lt;data&gt;
                 &lt;NAME&gt;two&lt;/NAME&gt;
                 &lt;PHONE&gt;two ph&lt;/PHONE&gt;
             &lt;/data&gt;
             &lt;data&gt;
                 &lt;NAME&gt;three&lt;/NAME&gt;
                 &lt;PHONE&gt;three ph&lt;/PHONE&gt;
             &lt;/data&gt;
         &lt;/root&gt;

    For Each data As XElement In xe.&lt;data&gt;
        ListView1.Items.Add(String.Format(&quot;{0}  {1}&quot;, data.&lt;NAME&gt;.Value, data.&lt;PHONE&gt;.Value))
    Next

huangapple
  • 本文由 发表于 2023年6月5日 15:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定