我有一系列格式相同的XML文件。我需要解析并提取某些标签的值。这是我的vb代码(注意到错误):
Dim dt As New DataTable
Dim newRow As DataRow
Dim ds As New DataSet
Dim doc As New Xml.XmlDocument
doc.Load(TextBox1.Text)
dt.Columns.Add("")
dt.Columns.Add("")
dt.Columns.Add("")
dt.Columns.Add("")
For Each n As XmlNode In doc.SelectNodes("LOG/TYPE")
newRow = dt.NewRow
newRow(n.Attributes("EVT").Value) = n.InnerText '-> Error: Object reference not set to an instance of an object.
Next
DataGridView1.DataSource = dt
以下是我尝试解析的XML文件的布局:
<LOG>
<TYPE>
<CLASS>class</CLASS>
<CODE>code</CODE>
<EVT>evt</EVT>
<SEVERITY>sev</SEVERITY>
<LABEL>label</LABEL>
<ORIGIN>origin</ORIGIN>
<USERNUMBER>user</USERNUMBER>
<USERTEXT>text</USERTEXT>
</TYPE>
<TYPE>
<CLASS>class1</CLASS>
<CODE>code1</CODE>
<EVT>evt1</EVT>
<SEVERITY>sev1</SEVERITY>
<LABEL>label1</LABEL>
<ORIGIN>origin1</ORIGIN>
<USERNUMBER>user1</USERNUMBER>
<USERTEXT>text1</USERTEXT>
</TYPE>
</LOG>
每个数据网格行需要的标记值如下:
<EVT>
<SEVERITY>
<LABEL>
<ORIGIN>
提前致谢!