Thursday, November 02, 2006

Using Resource Files

Recently, I had a case to use an XML embedded resource file. After adding the following XML file to the solution and setting its 'Build Action' property to 'Embedded Resource'...

<?xml version="1.0" encoding="utf-8" ?>
<Config>
    <VariablefileName>../Projects/RealEstatePostcard/PostCard.xvp</VariablefileName>
    <ProjectFileForVariableEvaluation>../Projects/RealEstatePostcard/PostCard.pf</ProjectFileForVariableEvaluation>
    <JobForVariableEvaluation>PDF_Job</JobForVariableEvaluation>
    <ShowScrollBars>Yes</ShowScrollBars>
    <BevelThickness>1</BevelThickness>
    <BevelThicknessInset>3</BevelThicknessInset>
    <BevelColorInset>lightGray</BevelColorInset>
    <BevelColorTopLeft>lightGray</BevelColorTopLeft>
    <BevelColorBottomRight>darkGray</BevelColorBottomRight>
    <uiConfiguration> PanelPostCardConfig.xml</uiConfiguration>
    <EditImage>Edit Image: Crop and Scale Your Image</EditImage>
    <bitmapUpdateMethod>xor</bitmapUpdateMethod>
</Config>

I grabbed a hold of it and set its contents against relevant public property names within my class. Like so:

private void LoadConfigValues()
        {
            // Create a new XmlDocument object and select the appropriate values.          
           
            // Reference the running assembly.
            Assembly thisAssembly = Assembly.GetExecutingAssembly();
       
            Stream oStrRead = thisAssembly.GetManifestResourceStream("Burrows.PageFlex.WYSIWYG.Config.Configuration.xml");
           

            XmlDocument xDoc = new XmlDocument();

            try
            {               
                xDoc.Load(oStrRead);
            }
            catch (IOException ioEx)
            {
                throw ioEx;
            }
            finally
            {               
                oStrRead.Close();
                oStrRead.Dispose();
            }

            // Loop through doc nodes and assign values to public properties.
            XmlNodeList oNodeList = xDoc.SelectNodes("//Config/*");
            foreach (XmlNode xN in oNodeList)
            {
                switch (xN.Name)
                {
                    case "VariablefileName":
                        this.strVariableFileName = xN.InnerText;
                        break;
                    case "ProjectFileForVariableEvaluation":
                        this.strProjectFileForVariableEvaluation = xN.InnerText;
                        break;
                    case "JobForVariableEvaluation":
                        this.strJobForVariableEvaluation = xN.InnerText;
                        break;
                    case "ShowScrollBars":
                        this.strShowScrollBars = xN.InnerText;
                        break;
                    case "BevelThickness":
                        this.strBevelThickness = xN.InnerText;
                        break;
                    case "BevelThicknessInset":
                        this.strBevelThicknessInset = xN.InnerText;
                        break;
                    case "BevelColorInset":
                        this.strBevelColorInset = xN.InnerText;
                        break;
                    case "BevelColorTopLeft":
                        this.strBevelColorTopLeft = xN.InnerText;
                        break;
                    case "BevelColorBottomRight":
                        this.strBevelColorBottomRight = xN.InnerText;
                        break;
                    case "uiConfiguration":
                        this.strUiConfiguration = xN.InnerText;
                        break;
                    case "EditImage":
                        this.strEditImage = xN.InnerText;
                        break;
                    case "bitmapUpdateMethod":
                        this.strBitmapUpdateMethod = xN.InnerText ;
                        break;
                    default:
                        break;
                };
            }          
        }

No comments:

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Z