1
我試圖從具有相同的名稱,但這裏不同的ID兩個XML元素得到不同的價值觀是我的代碼:System.Data.DataRowView返回XML沒有價值
<?xml version="1.0" encoding="utf-8"?>
<Vehicle_data>
<Data ID="1">
<Vehicle_name>bullet</Vehicle_name>
<Owner_name>qwe</Owner_name>
<Number_plate>q2236</Number_plate>
<Insurance_laps_date>qwe</Insurance_laps_date>
<Chassis_no>wqe</Chassis_no>
<Driving_lic_no>q</Driving_lic_no>
<Entry_date>05-09-15</Entry_date>
<Entry_time>08:36:52</Entry_time>
</Data>
<Data ID="2">
<Vehicle_name>optra</Vehicle_name>
<Owner_name>ott</Owner_name>
<Number_plate>qwerte</Number_plate>
<Insurance_laps_date>gdsfg</Insurance_laps_date>
<Chassis_no>dfgdf</Chassis_no>
<Driving_lic_no>dfgdf</Driving_lic_no>
<Entry_date>05-09-15</Entry_date>
<Entry_time>08:38:34</Entry_time>
</Data>
<Data ID="3">
<Vehicle_name>enzo</Vehicle_name>
<Owner_name>asd</Owner_name>
<Number_plate>awda</Number_plate>
<Insurance_laps_date>adaw</Insurance_laps_date>
<Chassis_no>awdw</Chassis_no>
<Driving_lic_no>adaw</Driving_lic_no>
<Entry_date>05-09-15</Entry_date>
<Entry_time>08:40:26</Entry_time>
</Data>
<Data ID="4">
<Vehicle_name>enzo</Vehicle_name>
<Owner_name>kok</Owner_name>
<Number_plate>asdasd</Number_plate>
<Insurance_laps_date>asfdadsad</Insurance_laps_date>
<Chassis_no>dasdasd</Chassis_no>
<Driving_lic_no>asdasdas</Driving_lic_no>
<Entry_date>05-09-15</Entry_date>
<Entry_time>08:42:05</Entry_time>
</Data>
</Vehicle_data>
這個我用下面的代碼的VB當窗體加載
Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.Columns.Add("ID")
For Each itm In doc.Descendants("Vehicle_name")
Dim dr As DataRow = dt.NewRow
dr("Name") = itm.Value
dr("ID") = itm.Parent.FirstAttribute.Value
dt.Rows.Add(dr)
Next
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"
在ComboBox1.SelectedIndexChanged
事件:
Dim doc As XDocument = XDocument.Load(Environment.CurrentDirectory & "\Vehicle_data.xml")
If ComboBox1.SelectedItem.ToString <> "System.Data.DataRowView" AndAlso _
ComboBox1.SelectedValue.ToString <> "System.Data.DataRowView" Then
Dim vehicledata = From el In doc.Element("Vehicle_data").Elements("Data") _
Where (el.Element("Vehicle_name") = ComboBox1.SelectedItem.ToString) _
Select New With
{
.VName = el.Element("Vehicle_name").Value, _
.onr = el.Element("Owner_name").Value, _
.nopl = el.Element("Number_plate").Value, _
.ins = el.Element("Insurance_laps_date").Value, _
.chassis = el.Element("Chassis_no").Value, _
.lic = el.Element("Driving_lic_no").Value, _
.PID = el.Element("Chassis_no").Parent.FirstAttribute.Value
}
For Each el In vehicledata
If ComboBox1.SelectedValue.ToString = el.PID Then
TextBox5.Text = el.VName
TextBox1.Text = el.onr
TextBox2.Text = el.nopl
TextBox3.Text = el.ins
TextBox4.Text = el.chassis
TextBox6.Text = el.lic
End If
Next
End If
我認爲el.PID
沒有返回任何值。 請幫幫我。
謝謝你sir davmos不僅爲你的答覆,而且還爲我指導和這樣一個很好的答案。是的,我使用if語句,以避免數據表空值導致應用程序崩潰。 – coc9