Convert Value From String - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Convert Value From String

Demo Code


using System.Text;
using System.Reflection;
using System.Xml;
using System.Collections.Generic;
using System.Collections;
using System.Net;
using System.Drawing;
using System;/*w w w. jav a  2  s  .  com*/

public class Main{
        /// <summary>
      /// 
      /// </summary>
      /// <param name="type"></param>
      /// <param name="stringValue"></param>
      /// <returns></returns>
      private static object GetValueFromString(Type type, string stringValue)
      {
         object valueObject;

         if (type.IsEnum)
         {
            valueObject = Enum.Parse(type, stringValue, true);
            }
         else
         {
            switch (type.Name)
            {
               case "Byte":
                  valueObject = byte.Parse(stringValue);
                  break;

               case "Int32":
                  valueObject = int.Parse(stringValue);
                  break;

               case "Single":
                  valueObject = float.Parse(stringValue);
                  break;

               case "Double":
                  valueObject = double.Parse(stringValue);
                  break;

               case "Boolean":
                  valueObject = bool.Parse(stringValue);
                  break;

               case "Color":
                  valueObject = ParseColor(stringValue);
                  break;

               case "IPAddress":
                  valueObject = IPAddress.Parse(stringValue);
                  break;

               case "Point":
                  valueObject = ParsePoint(stringValue);
                  break;

               case "DateTime":
#if WINDOWS
                  valueObject = DateTime.FromBinary(long.Parse(stringValue));
#else
                  valueObject = new DateTime(long.Parse(stringValue));
#endif
                  break;

               case "TimeSpan":
                  valueObject = TimeSpan.FromTicks(long.Parse(stringValue));
                  break;

               default:
                  valueObject = stringValue;
                  break;
            }
         }

         return valueObject;
      }
}

Related Tutorials