XML Werte in Listview einfügen

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Azumino.

    XML Werte in Listview einfügen

    Hallo liebe Community,

    Ich habe ein kleines Problem!
    Ich möchte aus einer XML Datei die Werte auslesen zb:

    XML-Quellcode

    1. <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    2. <?xml-stylesheet type="text/xsl" href="sms.xsl"?>
    3. <smses count="464">
    4. <sms protocol="0" address="******" date="*******" type="***" subject="null" body="*********" toa="null" sc_toa="null" service_center="null" read="1" status="0" locked="0" date_sent="null" readable_date="********" contact_name="********" />
    5. </smses>


    Ich möchte jetzt folgende Daten haben: address, date, type, subject, body, read, status, locked,readable_date, contact_name
    Aber iwie weiss ich nicht wie ich an diese Werte komme.

    Habe mal das gefunden:

    VB.NET-Quellcode

    1. Imports System.IO
    2. Public Class Form1
    3. Inherits System.Windows.Forms.Form
    4. Vom Windows Form Designer generierter Code
    5. Private xmlPath As String = _
    6. System.Configuration.ConfigurationSettings. _
    7. AppSettings.Get("xmlPath")
    8. Dim ds As New DataSet
    9. Private Sub Button1_Click(ByVal sender As System.Object, _
    10. ByVal e As System.EventArgs) Handles Button1.Click
    11. Read_XMLFile(xmlPath)
    12. End Sub
    13. Private Sub Read_XMLFile(ByVal sFile As String)
    14. Label1.Visible = True
    15. Label1.Refresh()
    16. Cursor = Cursors.WaitCursor
    17. ds.Clear()
    18. ListView1.Items.Clear()
    19. ds.ReadXml(sFile)
    20. RefreshResults("true")
    21. Cursor = Cursors.Default
    22. Label1.Visible = False
    23. End Sub
    24. Private Sub RefreshResults(ByVal filter As String)
    25. Try
    26. Dim table As DataTable = ds.Tables(0)
    27. Dim rows As DataRow() = table.Select(filter)
    28. For Each row As DataRow In rows
    29. Dim item As ListViewItem = _
    30. New ListViewItem(row("name").ToString())
    31. item.SubItems.Add(row("vorname").ToString())
    32. item.SubItems.Add(row("firma").ToString())
    33. item.SubItems.Add(row("beruf").ToString())
    34. item.SubItems.Add(row("strasse").ToString())
    35. item.SubItems.Add(row("wohnort").ToString())
    36. item.SubItems.Add(row("telefon").ToString())
    37. ListView1.Items.Add(item)
    38. Next
    39. Catch e As Exception
    40. MessageBox.Show(e.Message)
    41. End Try
    42. End Sub
    43. Private Sub Form1_Load(ByVal sender As System.Object, _
    44. ByVal e As System.EventArgs) Handles MyBase.Load
    45. Label2.Text = xmlPath
    46. CreateListview()
    47. End Sub
    48. Private Sub CreateListview()
    49. With ListView1
    50. .View = View.Details
    51. .Width = 745
    52. .Columns.Add("name", 100, HorizontalAlignment.Left)
    53. .Columns.Add("vorname", 100, HorizontalAlignment.Left)
    54. .Columns.Add("firma", 110, HorizontalAlignment.Left)
    55. .Columns.Add("beruf", 100, HorizontalAlignment.Left)
    56. .Columns.Add("strasse", 100, HorizontalAlignment.Left)
    57. .Columns.Add("wohnort", 120, HorizontalAlignment.Left)
    58. .Columns.Add("telefon", 110, HorizontalAlignment.Left)
    59. End With
    60. End Sub
    61. Private Sub Button2_Click(ByVal sender As System.Object, _
    62. ByVal e As System.EventArgs) Handles Button2.Click
    63. Me.Close()
    64. End Sub
    65. End Class


    Aber das geht auch nicht
    Danke für eure Antworten

    EDIT:// An diese Daten kommt mein Server der über SSH und spezial Software auf mein Handy zugreift und sich dann eine XML Datei erstellt. Das sollte dann eine SMS Datenbank sein, falls ich mein Handy verliere kann mein Server diese auf mein neues einfügen.
    Ich weiss das Doppelpost verboten sind aber hier die Lösung:

    VB.NET-Quellcode

    1. Public Class Form2
    2. Inherits MetroFramework.Forms.MetroForm
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4. Dim XMLDoc As New XmlDocument
    5. Dim Knoten As XmlNode
    6. XMLDoc.Load("***************************************")
    7. Dim Knotenliste As XmlNodeList = XMLDoc.SelectNodes("/smses/sms")
    8. For Each Knoten In Knotenliste
    9. Dim item As ListViewItem = _
    10. New ListViewItem(Knoten.Attributes("address").Value)
    11. item.SubItems.Add(Knoten.Attributes("type").Value)
    12. item.SubItems.Add(Knoten.Attributes("subject").Value)
    13. item.SubItems.Add(Knoten.Attributes("body").Value)
    14. item.SubItems.Add(Knoten.Attributes("contact_name").Value)
    15. c.Items.Add(item)
    16. Next
    17. End Sub
    18. End Class


    Ich dachte mir noch das ich es eig auch so machen kann :D