XML C#

#region Copyright (c) 2000-2010 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{                                                                   }
{                                                                   }
{       Copyright (c) 2000-2010 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2010 Developer Express Inc.
namespace DevExpress.Xpo.Data.Services {
    using System;
    using System.Diagnostics;
    using System.Xml;
    
    public static class XmlUtils {
        public static string GetValueString(object value) {
            Debug.Assert(value != null, "value != null");
            string result = null;
            Type valueType = value.GetType();
            valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;
            if (typeof(String) == valueType) {
                result = string.Format("'{0}'", ((string)value).Replace("'", "''"));
            } else if (typeof(Boolean) == valueType) {
                result = XmlConvert.ToString((bool)value);
            } else if (typeof(Byte) == valueType) {
                result = XmlConvert.ToString((byte)value);
            } else if (typeof(DateTime) == valueType) {
                result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
            } else if (typeof(Decimal) == valueType) {
                result = XmlConvert.ToString((decimal)value);
            } else if (typeof(Double) == valueType) {
                result = XmlConvert.ToString((double)value);
            } else if (typeof(Guid) == valueType) {
                result = value.ToString();
            } else if (typeof(Int16) == valueType) {
                result = XmlConvert.ToString((Int16)value);
            } else if (typeof(Int32) == valueType) {
                result = XmlConvert.ToString((Int32)value);
            } else if (typeof(Int64) == valueType) {
                result = XmlConvert.ToString((Int64)value);
            } else if (typeof(SByte) == valueType) {
                result = XmlConvert.ToString((SByte)value);
            } else if (typeof(Single) == valueType) {
                result = XmlConvert.ToString((Single)value);
            } else if (typeof(byte[]) == valueType) {
                byte[] byteArray = (byte[])value;
                result = Convert.ToBase64String(byteArray);
            } else if (typeof(System.Data.Linq.Binary) == valueType) {
                return GetValueString(((System.Data.Linq.Binary)value).ToArray());
            } else if (typeof(System.Xml.Linq.XElement) == valueType) {
                result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None);
            } else {
                result = null;
            }
            Debug.Assert(result != null, "result != null");
            return result;
        }
    }
}