This is a migrated thread and some comments may be shown as answers.

Embedding XML resource file

1 Answer 77 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
David
Top achievements
Rank 1
David asked on 03 May 2012, 05:09 PM
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:
  <?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;
        }
    }
}
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.
 

1 Answer, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 03 May 2012, 11:50 PM
Hello David,

I found a much easier method of loading the XML from the resource. Try this instead:

public List<vergeEnvironment> GetAllEnvironments()
{
    List<vergeEnvironment> environments = new List<vergeEnvironment>();
 
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(vsuiteautomation.Properties.Resources.Environments);
 
    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;
}

Regards,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
David
Top achievements
Rank 1
Answers by
Cody
Telerik team
Share this question
or