• 0

VB.Net XML, Help needed ?


Question

I have the code below in my project, my project generates an error on the ReadXml line, I have added a new Xml file to my project and renamed it to data.xml, how do I in the ReadXml line refer to the Xml file in my project ?

Dim ds As New DataSet
ds.ReadXml("data.xml")
Dim dt As DataTable = ds.Tables("stock")

' Add Row

Dim row As DataRow
row = dt.NewRow()
row("electrician") = electrician.SelectedItem
row("item") = item.SelectedItem
row("quantity") = quantity.Text
row("unitcost") = unitcost.Text
dt.Rows.Add(row)

' Write data back to xml file

ds.WriteXml("data.xml")

This is the error I recieve :

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.xml.dll

Additional information: Could not find file "D:\Programming\Visual Basic Net\VB Projects\DSR\bin\data.xml".

I expected when I added the Xml file to my project for it to be in the bin folder which is where the runtime code is kept but it not, its in the folder outside the bin folder.

When I build the program the Xml file does not get copied into the bin folder so the program can't find it.

Any help on how to reference my xml file in my project would be greatly appreciated ?

Kind Regards

Edited by Large
Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Why not try

Dim AppPath As String = Application.StartupPath 

Dim ds As New DataSet
ds.ReadXml(AppPath & "\data.xml")
...

if the data.xml is in the root directory of your app it should find it.

Link to comment
Share on other sites

  • 0

Yeah if I put it in my bin folder it works, i know that !

But I created the xml file by using the menu's in visual studio and selecting add component, so if the xml file is part of the project why does it not include it or copy it to the bin folder at run time ?

Kind Regards

Link to comment
Share on other sites

  • 0

[Totally off topic but im trying to get mine to read XML lol]

Could you post the XML file contents?

ChocIST

Link to comment
Share on other sites

  • 0
Yeah if I put it in my bin folder it works, i know that !

But I created the xml file by using the menu's in visual studio and selecting add component, so if the xml file is part of the project why does it not include it or copy it to the bin folder at run time ?

Kind Regards

Because the IDE isn't 100%. :unsure:

Sincerely, I've never found a way to get XML files to copy over automagically, unless it was an app.config file.

So, it's Show All Files, & Ctrl + L-Mouse + drag-n-drop in the Solution Explorer for you! :D

Link to comment
Share on other sites

  • 0
Because the IDE isn't 100%.  :unsure:

Sincerely, I've never found a way to get XML files to copy over automagically, unless it was an app.config file.

So, it's Show All Files, & Ctrl + L-Mouse + drag-n-drop in the Solution Explorer for you! :D

Now thats better, yeah I found that config files copied over but not xml files.

So is this a bug in Visual Studio Weenur or a small oversight by Microsoft ?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.