PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/QuickStarts/Modularity/Modularity.Tests.AcceptanceTests/Helpers/ResxConfigHandler.cs

#
C# | 75 lines | 45 code | 8 blank | 22 comment | 4 complexity | 9ad76ea3b0910d0076ef3ed38cfff4c6 MD5 | raw file
 1//===============================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation
 4//===============================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===============================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===============================================================================
17
18using System;
19using System.Collections.Generic;
20using System.Xml.XPath;
21using System.Resources;
22using System.Collections;
23using System.Globalization;
24
25namespace Modularity.AcceptanceTests.Helpers
26{
27    /// <summary>
28    /// Handler to read value from XML file (having key-value pair) on specifying the XML file path and the key.
29    /// </summary>
30    public class ResXConfigHandler : IDisposable
31    {
32        ResXResourceReader rsxr;
33
34        public ResXConfigHandler(string filePath)
35        {
36            // Create a ResXResourceReader for the file items.resx.
37            rsxr = new ResXResourceReader(filePath);
38        }
39
40        public virtual string GetValue(string key)
41        {
42            // Iterate through the resources and display the contents to the console.
43            foreach (DictionaryEntry d in rsxr)
44            {
45                if(d.Key.ToString().Equals(key))
46                {
47                    return String.Format(CultureInfo.InvariantCulture, d.Value.ToString());
48                }
49            }
50            return String.Empty;
51        }
52
53        #region IDisposable Members
54
55        public void Dispose()
56        {
57            Dispose(true);
58            GC.SuppressFinalize(this);
59        }
60
61        #endregion
62
63        protected virtual void Dispose(bool disposing)
64        {
65            if (disposing)
66            {
67                if (null != rsxr)
68                {
69                    //Close the reader.
70                    rsxr.Close();
71                }
72            }
73        }
74    }
75}