Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file compile-time in C#

Tags:

c#

compilation

I am writing some unit tests in which I need a fake xml file. I can create that file and require it to be deployed with the unit tests, but experience shows that at my office it's a lot of headache. So I decided that the file will be created by UT's.

StreamWriter sw = new StreamWriter(testFileName);
sw.Write(contents);
sw.Close();

Now the problem is the contents string. It is virtually a long xml, something like this:

string contents = 
@"<?xml version="1.0" encoding="windows-1251"?>
  <blah>
  ~100 lines here
  </blah> ";

I don't want this to be in the same file as the rest of the code. I want the string to be generated compile-time from a file.

In C++, I'd do this

string contents = "
#include "test.xml"
   ";

Is it possible somehow in C#?

like image 598
Armen Tsirunyan Avatar asked Jul 07 '11 15:07

Armen Tsirunyan


2 Answers

Why don't you include it in a resource?

like image 103
Daniel A. White Avatar answered Sep 18 '22 11:09

Daniel A. White


Use a resources (resx file) and add your xml file. Then you can access using MyNs.MyResources.MyXmlFile.

like image 30
Jeff Avatar answered Sep 20 '22 11:09

Jeff