Вопрос

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
Это было полезно?

Решение

Try to change relevant part of your code to this :

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

Summary of what has been fixed above :

  1. your XPath shouldn't end with slash (/)
  2. node name is "row" instead of "rowid", and you can use .GetAttribute() function to get attribute value of an XmlNode
  3. the rest of elements you're trying to select are child of <row> element, hence you need to append node("row") before
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top