Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(filename);
- XPathNavigator nav = xmlDoc.CreateNavigator();
- XPathNavigator navRoot = nav.SelectSingleNode(ElemContext);
- string EnvName = GetXMLObject<string>(navRoot, "@" + ElemEnvironmentName);
- private T GetXMLObject<T>(XPathNavigator navRoot, string name)
- {
- if (navRoot.SelectSingleNode(name) == null)
- {
- throw new NotImplementedException(string.Format("The specified property [{0}] does not exist in the xml node. Please re-check your xml.", name));
- }
- object item = navRoot.SelectSingleNode(name).Value;
- if (typeof(T).Equals(typeof(string)))
- {
- return (T)item;
- }
- if (typeof(T).Equals(typeof(int)))
- {
- item = Convert.ToInt32(item);
- return (T)item;
- }
- if (typeof(T).Equals(typeof(IntPtr)))
- {
- // The Convert must be big enough to convert a pointer from a x64 system.
- item = new IntPtr(Convert.ToInt64(item));
- return (T)item;
- }
- if (typeof(T).Equals(typeof(double)))
- {
- item = Convert.ToDouble(item);
- return (T)item;
- }
- if (typeof(T).Equals(typeof(DateTime)))
- {
- item = DateTime.Parse((string)item, null);
- return (T)item;
- }
- if (typeof(T).Equals(typeof(bool)))
- {
- item = Convert.ToBoolean(Convert.ToInt32(item));
- return (T)item;
- }
- if (typeof(T).Equals(typeof(Guid)))
- {
- item = new Guid((string)item);
- return (T)item;
- }
- if (typeof(T).BaseType.Equals(typeof(Enum)))
- {
- item = Enum.Parse(typeof(T), (string)item);
- return (T)item;
- }
- if (typeof(T).Equals(typeof(CultureInfo)))
- {
- item = CultureInfo.GetCultureInfoByIetfLanguageTag((string)item);
- return (T)item;
- }
- if (typeof(T).Equals(typeof(Rect)))
- {
- item = new RectConverter().ConvertFromString((string)item);
- return (T)item;
- }
- throw new NotImplementedException(string.Format("The datatype [{0}] has not been catered for.", typeof(T).Name));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement