This question is locked. New answers and comments are not allowed.
I added a resource file to my project to help me manage database connections for different test environments. I added an XML file to my project in Visual Studio (VS project > right click > Properties > Resources > Add Resource). The XML file is an embedded text document in my project. An earlier assumption I had is that the file is deployed to the output directory (bin\Debug) but I found out that that is not the case.
My XML file looks something like this:
When the XML file is a resource in my project I can refer to it directly (<projectname>.Properties.Resources.<filename>) but it is a string that needs to be loaded into a System.IO.MemoryStream object that can then be read by XmlDocument.Load.
Using the following article as an example: http://www.velocityreviews.com/forums/t296139-read-xml-from-string-instead-of-file-c.html, my code looks something like this:
There is a custom object called vergeEnvironment which is a custom object to house the resulting list of environments.
I hope this helps people out.
My XML file looks something like this:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
- <
Environments
>
- <
Environment
>
<
Name
>Rapid (Public)</
Name
>
<
URL
>ENTER YOUR URL HERE</
URL
>
<
ConnectionString
>ENTER YOUR CONNECTION STRING HERE</
ConnectionString
>
<
EnableUrlHashing
>1</
EnableUrlHashing
>
</
Environment
>
- <!--
Add any other environments you wish here.
Environments will be listed in the order they are found in the file.
-->
- <!--
<
Environment
>
<
Name
></
Name
>
<
URL
></
URL
>
<
ConnectionString
></
ConnectionString
>
<
EnableUrlHashing
></
EnableUrlHashing
>
</
Environment
>
-->
</
Environments
>
When the XML file is a resource in my project I can refer to it directly (<projectname>.Properties.Resources.<filename>) but it is a string that needs to be loaded into a System.IO.MemoryStream object that can then be read by XmlDocument.Load.
Using the following article as an example: http://www.velocityreviews.com/forums/t296139-read-xml-from-string-instead-of-file-c.html, my code looks something like this:
using
System.Collections.Generic;
using
System.Configuration;
using
System.Xml;
using
System.IO;
namespace
vsuiteautomation
{
public
class
EnvironmentFactory
{
public
EnvironmentFactory()
{
}
public
List<vergeEnvironment> GetAllEnvironments()
{
List<vergeEnvironment> environments =
new
List<vergeEnvironment>();
XmlDocument xml =
new
XmlDocument();
//ADDED THIS CODE TO CREATE THE MEMORYSTREAM
byte
[] byteArray =
new
byte
[vsuiteautomation.Properties.Resources.Environments.Length];
System.Text.ASCIIEncoding encoding =
new
System.Text.ASCIIEncoding();
byteArray = encoding.GetBytes(vsuiteautomation.Properties.Resources.Environments);
MemoryStream memoryStream =
new
MemoryStream(byteArray);
memoryStream.Seek(0, SeekOrigin.Begin);
xml.Load(memoryStream);
XmlNodeList environmentNodes = xml.SelectNodes(@
"//Environment"
);
if
(environmentNodes !=
null
)
{
foreach
(XmlNode environmentNode
in
environmentNodes)
{
XmlNode nameNode = environmentNode.SelectSingleNode(@
"Name"
);
XmlNode urlNode = environmentNode.SelectSingleNode(@
"URL"
);
XmlNode connectionStringNode = environmentNode.SelectSingleNode(@
"ConnectionString"
);
XmlNode enableUrlHashingNode = environmentNode.SelectSingleNode(@
"EnableUrlHashing"
);
if
(nameNode !=
null
&& urlNode !=
null
&& connectionStringNode !=
null
&& enableUrlHashingNode !=
null
)
{
bool
enableUrlHashing =
false
;
if
(enableUrlHashingNode.InnerText ==
"1"
)
{
enableUrlHashing =
true
;
}
environments.Add(
new
vergeEnvironment(nameNode.InnerText, urlNode.InnerText, enableUrlHashing, connectionStringNode.InnerText));
}
}
}
return
environments;
}
}
}
I hope this helps people out.