遍历XML元素并添加到访问表

时间:2019-05-15 09:04:48

标签: xml vb.net ms-access

嘿,我试图遍历XML的元素,以便将每个元素的内容添加到访问表中。这是我正在使用的XML文件的快照:

-<XML>
-<cards>
<name>Majespecter Toad - Ogama</name>
<type>Pendulum Effect Monster</type>
<desc>When this card is Normal or Special Summoned: You can Set 1 "Majespecter" Spell/Trap Card directly from your Deck, but it cannot activate this turn. You can only use this effect of "Majespecter Toad - Ogama" once per turn. Cannot be targeted or destroyed </desc>
<race>Spellcaster</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/645794.jpg</image_url>
<atk>1300</atk>
<def>500</def>
<level>4</level>
</cards>

-<cards>
<name>Gladiator Beast Retiari</name>
<type>Effect Monster</type>
<desc>When this card is Special Summoned by the effect of a "Gladiator Beast" monster, you can remove from play 1 card from your opponent's Graveyard. At the end of the Battle Phase, if this card attacked or was attacked, you can return it to the Deck to Specia</desc>
<race>Aqua</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/612115.jpg</image_url>
<atk>1200</atk>
<def>800</def>
<level>3</level>
</cards>

-<cards>
<name>Malefic Rainbow Dragon</name>
<type>Effect Monster</type>
<desc>This card cannot be Normal Summoned or Set. This card cannot be Special Summoned, except by removing from play 1 "Rainbow Dragon" from your hand or Deck. There can only be 1 face-up "Malefic" monster on the field. Other monsters you control cannot declare</desc>
<race>Dragon</race>
<image_url>https://storage.googleapis.com/ygoprodeck.com/pics/598988.jpg</image_url>
<atk>4000</atk>
<def>0</def>
<level>10</level>
</cards>

这是我的代码,应该遍历XML文件。我遇到的错误是:对象引用未设置为对象的实例。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ''parse your XML into an XDocument
        Dim doc As XDocument = XDocument.Load("C:\Users\1964\Documents\Visual Studio 2015\Projects\YGO Access Test v10\bin\Debug\MyData.xml")
        '' get "Install" element
        Dim install As XElement = doc.Root

        Dim conn As New OleDbConnection
        conn = New OleDbConnection
        dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
        Dim databasePath = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
        conn.ConnectionString = dbprovider & databasePath
        Dim command As OleDbCommand
        Try
            conn.Open()
            command = New OleDbCommand()

            For Each cards As XElement In install.Elements("cards")
                Dim name As String = cards.Element("name").Value
                Dim type As String = cards.Element("type").Value
                Dim desc As String = cards.Element("desc").Value
                Dim race As String = cards.Element("race").Value
                Dim image_url As String = cards.Element("image_url").Value
                Dim atk As String = cards.Element("atk").Value
                Dim def As String = cards.Element("def").Value
                Dim level As String = cards.Element("level").Value
                MsgBox(name & " " & type & " " & desc)

                command.Parameters.AddWithValue("@name", Convert.ToString(name))
                command.Parameters.AddWithValue("@type", Convert.ToString(type))
                command.Parameters.AddWithValue("@desc", Convert.ToString(desc))
                command.Parameters.AddWithValue("@race", Convert.ToString(race))
                command.Parameters.AddWithValue("@image_url", Convert.ToString(image_url))
                command.Parameters.AddWithValue("@atk", Convert.ToInt32(atk))
                command.Parameters.AddWithValue("@def", Convert.ToInt32(def))
                command.Parameters.AddWithValue("@level", Convert.ToInt32(level))

                command.Connection = conn
                command.CommandText = "insert into [" & decknametxt.Text & "] (name, type, [desc], race, image_url, atk, def, [level]) values(@name, @type, @desc, @race, @image_url, @atk, @def, @level)"
             command.ExecuteNonQuery()
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub 

1 个答案:

答案 0 :(得分:0)

您的XML看起来有点畸形...应该看起来像这样。

<XML>
    <cards>
        <name>Majespecter Toad - Ogama</name>
        <type>Pendulum Effect Monster</type>
        <desc>When this card is Normal or Special Summoned: You can Set 1 "Majespecter" Spell/Trap Card directly from your Deck, but it cannot activate this turn. You can only use this effect of "Majespecter Toad - Ogama" once per turn. Cannot be targeted or destroyed </desc>
        <race>Spellcaster</race>
        <image_url>https://storage.googleapis.com/ygoprodeck.com/pics/645794.jpg</image_url>
        <atk>1300</atk>
        <def>500</def>
        <level>4</level>
    </cards>
    <cards>
        <name>Gladiator Beast Retiari</name>
        <type>Effect Monster</type>
        <desc>When this card is Special Summoned by the effect of a "Gladiator Beast" monster, you can remove from play 1 card from your opponent's Graveyard. At the end of the Battle Phase, if this card attacked or was attacked, you can return it to the Deck to Specia</desc>
        <race>Aqua</race>
        <image_url>https://storage.googleapis.com/ygoprodeck.com/pics/612115.jpg</image_url>
        <atk>1200</atk>
        <def>800</def>
        <level>3</level>
    </cards>
    <cards>
        <name>Malefic Rainbow Dragon</name>
        <type>Effect Monster</type>
        <desc>This card cannot be Normal Summoned or Set. This card cannot be Special Summoned, except by removing from play 1 "Rainbow Dragon" from your hand or Deck. There can only be 1 face-up "Malefic" monster on the field. Other monsters you control cannot declare</desc>
        <race>Dragon</race>
        <image_url>https://storage.googleapis.com/ygoprodeck.com/pics/598988.jpg</image_url>
        <atk>4000</atk>
        <def>0</def>
        <level>10</level>
    </cards>
</XML>

我个人将XML直接加载到数据集中,然后遍历数据表。像这样...

   Dim conn As New OleDbConnection
   conn = New OleDbConnection
   dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
   Dim databasePath = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
   conn.ConnectionString = dbprovider & databasePath
   Dim command As OleDbCommand
   Try

       Dim f as XmlReader= XmlReader.Create("C:\Users\1964\Documents\Visual Studio 2015\Projects\YGO Access Test v10\bin\Debug\MyData.xml", New XmlReaderSettings())

       Dim ds as new DataSet
       ds.readxml(f)
       conn.Open()
       command = New OleDbCommand()

            For each row as datarow in ds.tables(0).rows
                Dim name As String = row.item("name")
                Dim type As String = row.item("type")
                Dim desc As String = row.item("desc")
                Dim race As String = row.item("race")
                Dim image_url As String = row.item("image_url")
                Dim atk As String = row.item("atk")
                Dim def As String = row.item("def")
                Dim level As String = row.item("level")
                MsgBox(name & " " & type & " " & desc)

                command.Parameters.AddWithValue("@name", Convert.ToString(name))
                command.Parameters.AddWithValue("@type", Convert.ToString(type))
                command.Parameters.AddWithValue("@desc", Convert.ToString(desc))
                command.Parameters.AddWithValue("@race", Convert.ToString(race))
                command.Parameters.AddWithValue("@image_url", Convert.ToString(image_url))
                command.Parameters.AddWithValue("@atk", Convert.ToInt32(atk))
                command.Parameters.AddWithValue("@def", Convert.ToInt32(def))
                command.Parameters.AddWithValue("@level", Convert.ToInt32(level))

                command.Connection = conn
                command.CommandText = "insert into [" & decknametxt.Text & "] (name, type, [desc], race, image_url, atk, def, [level]) values(@name, @type, @desc, @race, @image_url, @atk, @def, @level)"
                command.ExecuteNonQuery()
           Next
    Catch ex as Exception
         MsgBox(ex.Message)
    End Try

如果数据有问题,则可以在Debug模式下轻松查看数据集,或在datagridview中显示数据集。 (datagridview1.datasource = ds.tables(0))

如果XML格式错误,则异常将指示其格式错误。