我正在尝试创建一个从游戏资源文件中读取 xml 文件的小型桌面应用程序(使用 Visual Studio)。这些文件存储在 XML 中。当我尝试将文件读入 datagridview 时,我收到一个错误:
System.ArgumentException: Child list for field SpaceUnitsCapital cannot be created.
at System.Windows.Forms.BindingContext.EnsureListManager(Object dataSource, String dataMember)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.SetDataConnection(Object dataSource, String dataMember)
at System.Windows.Forms.DataGridView.set_DataMember(String value)
at SWFOC.Form1.clk(Object sender, EventArgs e) in C:\Users\Number6\documents\visual studio 2012\Projects\SWFOC\SWFOC\Form1.vb:line 37
我用来尝试的代码是:
Dim URL As String = "C:\Program Files (x86)\LucasArts\Star Wars Empire at War Forces of Corruption\Data\XML\Spaceunitscapital.xml"
Dim ds As DataSet = New DataSet()
Dim fsReadXml As System.IO.FileStream = New System.IO.FileStream(URL, System.IO.FileMode.Open)
Try
ds.ReadXml(fsReadXml)
DataGridView1.DataSource = ds
DataGridView1.DataMember = "SpaceUnitsCapital"
Catch Err As Exception
RichTextBox1.Text = Err.ToString
Finally
fsReadXml.Close()
End Try
以下是 xml 文件的示例:
<?xml version="1.0"?>
<SpaceUnitsCapital>
<SpaceUnit Name="Calamari_Cruiser">
<Text_ID>TEXT_UNIT_CALAMARI_CRUISER</Text_ID>
<Encyclopedia_Good_Against> Victory_Destroyer Acclamator_Assault_Ship Interceptor4_Frigate </Encyclopedia_Good_Against>
<Encyclopedia_Vulnerable_To> TIE_Bomber Skipray_Blastboat </Encyclopedia_Vulnerable_To>
<GUI_Row> 1 </GUI_Row>
<Space_Model_Name>RV_MONCALCRUISER.ALO</Space_Model_Name>
<Select_Box_Scale>600</Select_Box_Scale>
<Select_Box_Z_Adjust>-50</Select_Box_Z_Adjust>
<Mass>0.995</Mass>
<Scale_Factor>1.1</Scale_Factor>
<Damage>60</Damage>
<Dense_FOW_Reveal_Range_Multiplier>0.24</Dense_FOW_Reveal_Range_Multiplier>
<Visible_On_Radar_When_Fogged>true</Visible_On_Radar_When_Fogged>
<Multisample_FOW_Check>Yes</Multisample_FOW_Check>
<Ranking_In_Category>5</Ranking_In_Category>
<Max_Speed>1.5</Max_Speed>
<Max_Rate_Of_Turn>0.4</Max_Rate_Of_Turn>
<MovementClass> Space </MovementClass>
<Space_Layer> Capital </Space_Layer>
<Layer_Z_Adjust>-290.0</Layer_Z_Adjust>
<OverrideAcceleration> .02 </OverrideAcceleration>
<OverrideDeceleration> .02 </OverrideDeceleration>
<Armor_Type> Armor_Calamari_Cruiser </Armor_Type>
<Shield_Armor_Type>Shield_Capital</Shield_Armor_Type>
<Max_Rate_Of_Roll>0.2</Max_Rate_Of_Roll>
<Bank_Turn_Angle>25</Bank_Turn_Angle>
<Max_Thrust>0.2</Max_Thrust>
<Hyperspace>Yes</Hyperspace>
<Hyperspace_Speed>1</Hyperspace_Speed>
<Maintenance_Cost>0.3</Maintenance_Cost>
</SpaceUnit>
</SpaceUnitsCapital>
如果我尝试在没有填充数据成员的行的情况下运行代码,它不会抛出错误,但它也不会做任何其他事情。它从不填充 DGV。
有谁知道我怎样才能避免这个错误?最终结果是以某种形式将字段填充到应用程序中,以便随后可以编辑和保存它们,因此如果有人有任何想法,我愿意接受其他方法。
在此先感谢,克里斯