2

[C# .NET 4.0 WinForm LINQ]

I had an XML file that I added to my project as Content, but I'm now having to make it an internal, embedded resource so it will compile with the executable and I won't have to create an install/deployment package for the app.

However, I have existing code that uses LINQ to query the XML file that won't work now that I've made the file an embedded resource. What do I need to do differently to be able to query the XML file if it's an embedded resource compared to when it's a content resource? I've seen some

Once I query the XML file, I loop through the results and load them into a list box. Here's the code I'm using to query the XML file when it's set to Content and my loop to add to the listbox:

var computers = from e in XElement.Load(@"MyXML.xml").Elements("computer")
        select (string)e.Element("name");

foreach (var c in computers)
{
    if (!IsNullOrEmpty(computer))
    {
        lstComputer.BeginUpdate();
        lstComputer.Items.Add(computer);
        lstComputer.EndUpdate();
    }
}

I've seen some other examples, like this one, that use the assembly to read the file into a string...is this what I would have to do? Not to further reveal my inner noob, but if that's the case, does the example in the link above return a delimited string that I can loop through so I can add the items to my listbox?

Thanks...

2 Answers 2

3

Open the embedded resource as a stream:

XElement doc;
using (var stream = typeof(SomeTypeInTheAssembly).Assembly
                        .GetManifestResourceStream("MyXML.xml"))
{
    doc = XElement.Load(stream);
}

There's no need to go via an intermediate string representation.

Sign up to request clarification or add additional context in comments.

12 Comments

This requires a reference to the IO and Reflection namespaces, right? Also, could you give a little more detail about SomeTypeInTheAssembly?
@user2063351: Nope, no using directives required. The point of SomeTypeInTheAssembly is just that you pick any type which is in the right assembly - you're just trying to specify which assembly you want to fetch the stream from. So the type that this code occurs in is probably fine, but I didn't know what that was called...
I'm sorry for being dense...would a valid typeof(SomeTypeInTheAssembly) be "XML"? If I'm creating a Windows Form, would I be best suited to use this.GetType().Assembly instead of typeof(SomeTypeInTheAssembly).Assembly?
@user2063351: Well I don't know - do you have a class called XML in the right assembly? You could use this.GetType() if it's in an instance method, sure.
Okay, I don't have either of those. How can I find the type to use in the assembly? When I set the XML file to be an embedded resource, should Visual Studio have added something to the AssemblyInfo.cs file?
|
0

Based on Jon Skeet's answer, you could use Assembly.GetExecutingAssembly() to make things a little bit easier:

using System.Reflection;

XElement doc;
using (var stream = Assembly.GetExecutingAssembly().
                    .GetManifestResourceStream("MyNameSpace.MyXML.xml"))
{
    doc = XElement.Load(stream);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.