469,365 Members | 1,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,365 developers. It's quick & easy.

Working with an Embedded XML File

Hello all,
I have a basic XML file of which I set the Build Action to "Embedded
Resource" The XML file is simple:

<root>
<item>
<name>Test Name</name>
<description>Test Description</description>
</item>
</root>

I'm having a heck of a time reading the xml file embedded in the assembly.
I'm more than certain that I'm missing some sort of name space element in
the XML.. I've searched high and low and I am unable to find any sample
code that works. I get errors such as invalid object, etc.

I was hoping that somebody might be able to refer me to a url that explains
how to create the xml file correctly and the code to read the embedded xml
file.

C# and vb.net examples are welcome.

Thanks in advance.

Jay

Nov 12 '05 #1
2 19178
If you've compiled the XML file as an embedded resource then you can do
something like this:
using System;
using System.IO;
using System.Reflection;
using System.Xml;

class Application
{
static void Main(string[] args) {

Stream s =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("CSharpConsole.XML
File1.xml");

XmlDocument xdoc = new XmlDocument();

StreamReader reader = new StreamReader(s);

xdoc.LoadXml(reader.ReadToEnd());

reader.Close();

}
}

or:

Imports System.Xml
Imports System.Reflection
Imports System.IO

Module Main

Sub Main()

Dim s As Stream =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("XMLFile1.xml")

Dim xdoc As New XmlDocument

Dim reader As New StreamReader(s)

xdoc.LoadXml(reader.ReadToEnd())

reader.Close()

End Sub

End Module

Notice in C# you have to prepend the project name (this is not a namespace)
to the file name. IIRC this is due to the way the VB compiler stores
resources in the assembly.

--
Klaus H. Probst, MVP
http://www.vbbox.com/
"Jay Douglas" <RE*****************@squarei.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello all,
I have a basic XML file of which I set the Build Action to "Embedded
Resource" The XML file is simple:

<root>
<item>
<name>Test Name</name>
<description>Test Description</description>
</item>
</root>

I'm having a heck of a time reading the xml file embedded in the assembly.
I'm more than certain that I'm missing some sort of name space element in
the XML.. I've searched high and low and I am unable to find any sample
code that works. I get errors such as invalid object, etc.

I was hoping that somebody might be able to refer me to a url that explains how to create the xml file correctly and the code to read the embedded xml
file.

C# and vb.net examples are welcome.

Thanks in advance.

Jay

Nov 12 '05 #2
Perfect.

Thanks a ton.
"Klaus H. Probst" <us*******@vbbox.com> wrote in message
news:eg**************@TK2MSFTNGP12.phx.gbl...
If you've compiled the XML file as an embedded resource then you can do
something like this:
using System;
using System.IO;
using System.Reflection;
using System.Xml;

class Application
{
static void Main(string[] args) {

Stream s =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("CSharpConsole.XML File1.xml");

XmlDocument xdoc = new XmlDocument();

StreamReader reader = new StreamReader(s);

xdoc.LoadXml(reader.ReadToEnd());

reader.Close();

}
}

or:

Imports System.Xml
Imports System.Reflection
Imports System.IO

Module Main

Sub Main()

Dim s As Stream =
Assembly.GetExecutingAssembly().GetManifestResourc eStream("XMLFile1.xml")

Dim xdoc As New XmlDocument

Dim reader As New StreamReader(s)

xdoc.LoadXml(reader.ReadToEnd())

reader.Close()

End Sub

End Module

Notice in C# you have to prepend the project name (this is not a namespace) to the file name. IIRC this is due to the way the VB compiler stores
resources in the assembly.

--
Klaus H. Probst, MVP
http://www.vbbox.com/
"Jay Douglas" <RE*****************@squarei.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello all,
I have a basic XML file of which I set the Build Action to "Embedded
Resource" The XML file is simple:

<root>
<item>
<name>Test Name</name>
<description>Test Description</description>
</item>
</root>

I'm having a heck of a time reading the xml file embedded in the assembly. I'm more than certain that I'm missing some sort of name space element in the XML.. I've searched high and low and I am unable to find any sample
code that works. I get errors such as invalid object, etc.

I was hoping that somebody might be able to refer me to a url that

explains
how to create the xml file correctly and the code to read the embedded xml file.

C# and vb.net examples are welcome.

Thanks in advance.

Jay


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Minh | last post: by
4 posts views Thread by Axter | last post: by
reply views Thread by andreas2411 | last post: by
reply views Thread by Belsirk | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.