簡體   English   中英

VB.Net XMLReader檢查值是否為空

[英]VB.Net XMLReader check if value is empty

我想創建一個程序,該程序讀取xml文件並將獲得的值放入DataGridView中。 該XML文件是MySQL數據庫的表轉儲。 在此表中,我有一個“設置”列,可以在其中選擇一些選項。 這是一個沒有選定選項的示例

<size></size>

並帶有選擇的選項

<size>SMALL,MEDIUM,LARGE,XLARGE</size>

這是我用來讀取xml文件的代碼:

    Dim ofile As New OpenFileDialog
    If ofile.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim reader As XmlTextReader = New XmlTextReader(ofile.FileName)
        Using reader
            Dim toadd As String = Nothing
            Dim str() As String
            Do While (reader.Read())
                Select Case reader.NodeType
                    Case XmlNodeType.Element
                        If reader.Name.ToLower.Contains("oldIndex") Then
                            Exit Select
                        End If
                    Case XmlNodeType.Text
                        If Not reader.Value = vbNullString Or Not reader.Value = vbNullChar Then
                            toadd = toadd & reader.Value & vbTab
                        Else
                            toadd = toadd & "no data" & vbTab
                        End If
                    Case XmlNodeType.EndElement
                        If reader.Name.Contains("RECORD") Then
                            If toadd IsNot Nothing Then
                                str = toadd.Split(vbTab)
                                ShopTable.Rows.Add(str)
                                toadd = Nothing
                            End If
                        End If
                End Select
            Loop
        End Using
    End If

size標簽之間有一些數據時,讀取沒有問題,但是如果沒有數據,那么它什么也不會讀取,因此我不會收到“沒有數據”的信息。 我該如何解決? 在此先感謝您的幫助。 親切的問候。

好吧,我不得不重寫我的算法。 我的xml文件包含這種格式的數據

<?xml version="1.0" standalone="yes"?>
 <RECORDS>
  <RECORD>
   <customerID>101</customerID>
   <name>Gall Anonim</name>
   <item>t-shirt</item>
   <size></size>
   ...
  </RECORD>
 </RECORDS>

我無法獲得size字段的值,這不是事件為空。 所以我使用下面的算法來獲取size元素的直接值;)

Dim ofile As New OpenFileDialog
If ofile.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim sr As New System.IO.StreamReader(ofile.FileName)
    Dim document As XDocument = XDocument.Parse(sr.ReadToEnd.ToString)
    Dim records = From record In document.Descendants("RECORD") _
                  Select New With _
                     { _
                        .cID = record.Element("customerID").Value, _
                        .name = Chr(&H22) & record.Element("name").Value & Chr(&H22), _
                        .item = record.Element("item").Value, _
                        .size = record.Element("size").Value _
                     }
    MsgBox(records.cID) 'shows customer id
    do some work with those variables
    ...
End If

問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM