2

I have an xml file, currently external to the C# code and it is loaded like this:

var xElem = XElement.Load("ProductTable.xml");

How do I "use" this file as an "embedded" element in my C# code instead of having it as an external file?

I know I am not clear enough with the words "use" and "embedded". What I want to do is to use it as some kind of variable such as

var myXML = /* content of the XML file here */;

just so that I don't have to use an external xml file. Thanks.

1
  • So you basically just want the xml content of the file assigned to a variable? Commented Dec 21, 2011 at 21:36

2 Answers 2

1

You have a resources file in your project. Just dragNdrop it there and you will be able to access it. Another way is to add this file to project, then click properties, set compile method to emedded and then do smth like this

try
   {
      _assembly = Assembly.GetExecutingAssembly();
      _imageStream = _assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp");
      _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyXmlFile.xml"));
   }
   catch
   {
      MessageBox.Show("Error accessing resources!");
   }
Sign up to request clarification or add additional context in comments.

Comments

1

Add the XML file to your project. Set its build setting (in properties via Solution Explorer) to Embedded Resource. Then you can use something like...

 var asm = Assembly.GetExecutingAssembly();
 using (var sr = new StreamReader(asm.GetManifestResourceStream("YourNamespace.ProductTable.xml")))
 { /* ... */ }

You'll probably want to use the XmlReader or whatever they call it these days.

Derived from How to embed and access... from MSDN.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.