Get Value String from Xml : DOM « XML « C# / C Sharp






Get Value String from Xml

  

#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;
        }
    }
}

   
    
  








Related examples in the same category

1.DOM feature check
2.Create Xml Document, Node
3.Load String from xml element
4.Load boolean value from xml element
5.Get integer value from xml element
6.Get attribute from XmlNode
7.Set Xml Node Value
8.Get InnerXml without changing the spacing
9.Get Inner Xml
10.Add Xml Element
11.Returns the InnerText value from a node or string.Empty if the node is null.
12.Sets the inner text in a node. If the node doesn't exist, it creates a new one and adds the text to it.