• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Add XML File as embedded resource file

I have some data I want preloaded into my VB app.  If I save it as an XML file, can I attach it as a resource file to use whenever my program is run?  
0
bengelhart
Asked:
bengelhart
  • 8
  • 4
1 Solution
 
carmodykCommented:
Yes, when you make your setup solution, add the XML file to along side the Solution Output in the application folder.   To access the XML file when the VB.NET application is loading, use this:

1. Declare "Imports System.IO" at the top of your VB Application

2. Access the file location:
Dim strFile as string = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) & "\MyXMLFile.xml"

as long as your XML file is in the same folder as the application executable, you shouldn't have any problems.  
0
 
arif_eqbalCommented:
If you are asking about embedding The XML file as a part of your Exe, so that you can just use the xCopy deployment and not bother about sending the xml file along you can as well achieve that
Add the XML File in your App and right click on the XML File in the Solution Explorer and go to Properties
In the Build Action Property set the value to "Embedded Resource"
Now in order to access the XML file you use Reflection, So Import System.Reflection and say

Dim DS as New Dataset
Dim ExecAssembly As [Assembly] = Me.GetType.Assembly.GetExecutingAssembly
Dim Str as IO.Stream = ExecAssembly.GetManifestResourceStream(ExecAssembly.GetName.Name.ToString & ".YourXMLFileName.xml") '<-- FileName here is Case Sensitive also remember it starts with a Dot (.)
DS.ReadXML(Str)
DataGrid1.DataSource=DS
0
 
bengelhartAuthor Commented:
Thanks!  That is exactly what I am looking for!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bengelhartAuthor Commented:
How do I import system.reflection?  I am using .NET 2003.  I right clicked on References to add it, but I didn't see it in the list
0
 
bengelhartAuthor Commented:
It only works if I have a copy of the XML file in the bin folder and I replace "Str" with the name of the XML file.    So Arif Eqbal, the embedded thing isn't working.  I will keep playing with it to see if I can figure it out.
0
 
arif_eqbalCommented:
No No in that case you are using the physical file path, though the file has been embedded you are not using the embedded file
No need to put any reference just say

Imports System.Reflection

at the top of your Page,

0
 
bengelhartAuthor Commented:
I still can't get it to work from the code you submitted.  I am trying to tweak it, but with no luck.  The dataset keeps coming up empty.  
0
 
arif_eqbalCommented:
OK So check the contents of your XML file
for instance, open the XML file in VS.NET At the bottom you'd see Two Views "XML" And "Data"
Click on Data and see how it shows the data there, your Datagrid should show the Data exactly as it is displayed here. (You might need to expand the view using the "+")
0
 
bengelhartAuthor Commented:
No I don't even get a "+".   It isn't passing anything to the "Str" stream variable.  Here is my code below, when it gets to the msgbox I get an error on the Str variable that says "Additional information: Object reference not set to an instance of an object.".

        Dim ds As New System.Data.DataSet
        Dim ExecAssembly As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly()
        Dim stName As String = ExecAssembly.GetName().ToString
        Dim Str As System.IO.Stream

        Str = ExecAssembly.GetManifestResourceStream(stName & ".ASQ.XML")
       

        If Not IsNothing(Str) Then
            ds.ReadXml(Str)
            dgList.DataSource = ds.Tables("qryxml") '.Tables("qryXML") 'ds.Tables("qryxml")
            dgList.Refresh()
        Else
            MsgBox("Nothing " & Str.Length)
        End If
0
 
arif_eqbalCommented:
Change
Dim stName As String = ExecAssembly.GetName().ToString
To
Dim stName As String = ExecAssembly.GetName.Name.ToString

Also remember the Name ".ASQ.XML" is case sensitive so the File name in the Solution Explorer should be exactly "ASQ.XML" i.e. Caps

0
 
bengelhartAuthor Commented:
Still nothing.  I have the case of the name of the file exactly as it is.  On the XML file I have Embedded Resouce selected, do I need to also put in my namespace in the "Custom tool namespace" field in the xml documents properties?
0
 
bengelhartAuthor Commented:
When I view the resources window my XML file is not there even though in its properties I have specified it is an embedded resource
0
 
bengelhartAuthor Commented:
Got it working.  I had to list out the resources in the assembly.  It turns out my namespace was different in the assembly than the name of the project.  The name of the project is NN-J-WI and in the assembly it is NN_J_WI.  If anyone needs the code to list your resources it is in the .NET MSDN help.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now