1

If I'm importing data such as this it will not work.

Debug also shows

"A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.dll"

<?xml version="1.0" encoding="utf-8" ?>
<XMLDocument>
  <Books>
    <Book>
      <row id="0">
    <data_rate_in_as_string>0B</data_rate_in_as_string>
    <data_rate_out_as_string>0B</data_rate_out_as_string>
    <downloaded_as_string>956.6MB</downloaded_as_string>
    <elapsed_as_string>174d2h48m49s</elapsed_as_string>
      </row>
    </Book>
    <Book>
      <row id="1">
    <data_rate_in_as_string>0B</data_rate_in_as_string>
    <data_rate_out_as_string>0B</data_rate_out_as_string>
    <downloaded_as_string>956.6MB</downloaded_as_string>
    <elapsed_as_string>174d2h48m49s</elapsed_as_string>
      </row>
    </Book>
  </Books>
</XMLDocument>

However if I removed the row ids completely it will work and I can import my data. I want to keep my row ids however and I also would like to add them to my datagridview too.

This is the project code as so far if it helps.

    Public Shared Function EmptyStringToNull(o As String) As Object
    Dim ret As Object = DBNull.Value
    Try
        If o.Trim.Length = 0 Then
            ret = DBNull.Value
        Else
            ret = o
        End If
    Catch ex As Exception
    End Try
    Return ret
End Function

Private Sub ReadXMLFile()
    Dim xmlDoc As New System.Xml.XmlDocument
    Dim root As XmlElement = Nothing
    Dim nodes As XmlNodeList = Nothing
    Dim node As XmlNode = Nothing
    Dim xmlFile As String = ""
    Try
        OpenFileDialog1.ShowDialog()
        xmlFile = OpenFileDialog1.FileName
        xmlDoc.Load(xmlFile)
        root = xmlDoc.DocumentElement
        nodes = root.SelectNodes("//XMLDocument/Books/Book/") 'The XMLPath
        'nodes = root.SelectNodes("//result/data/row") 'The XMLPath
        Me.DataGridView1.Rows.Clear() 'Clear Grid
        For Each node In nodes
            Me.DataGridView1.Rows.Add(EmptyStringToNull(node("rowid").InnerText),
                                      EmptyStringToNull(node("data_rate_in_as_string").InnerText),
                                      EmptyStringToNull(node("data_rate_out_as_string").InnerText),
                                      EmptyStringToNull(node("downloaded_as_string").InnerText),
                                      EmptyStringToNull(node("elapsed_as_string").InnerText))
        Next
    Catch ex As Exception
    End Try
End Sub
4

1 回答 1

0

尝试将代码的相关部分更改为:

nodes = root.SelectNodes("//XMLDocument/Books/Book") 'The XMLPath
Me.DataGridView1.Rows.Clear() 'Clear Grid
For Each node In nodes
    Me.DataGridView1.Rows.Add(EmptyStringToNull(node("row").GetAttribute("id")),
                              EmptyStringToNull(node("row")("data_rate_in_as_string").InnerText),
                              EmptyStringToNull(node("row")("data_rate_out_as_string").InnerText),
                              EmptyStringToNull(node("row")("downloaded_as_string").InnerText),
                              EmptyStringToNull(node("row")("elapsed_as_string").InnerText))
Next

上面已修复的内容摘要:

  1. 您的 XPath 不应以斜杠 ( /)结尾
  2. 节点名称是“row”而不是“rowid”,您可以使用 .GetAttribute()函数来获取一个属性值XmlNode
  3. 您尝试选择的其余元素是元素的子<row> 元素,因此您需要先node("row")附加
于 2014-03-26T08:46:55.140 回答