5 // Copyright 2007 Novell (http://www.novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections;
29 using System.Collections.Generic;
30 using System.Globalization;
35 using XPI = System.Xml.Linq.XProcessingInstruction;
37 namespace System.Xml.Linq
39 internal static class XUtil
41 public const string XmlnsNamespace =
42 "http://www.w3.org/2000/xmlns/";
44 public static bool ConvertToBoolean (string s)
46 return XmlConvert.ToBoolean (s.ToLower (CultureInfo.InvariantCulture));
49 public static DateTime ToDateTime (string s)
52 return XmlConvert.ToDateTime (s, XmlDateTimeSerializationMode.RoundtripKind);
54 return DateTime.Parse (s);
58 public static string ToString (object o)
61 throw new InvalidOperationException ("Attempt to get string from null");
63 switch (Type.GetTypeCode (o.GetType ())) {
66 case TypeCode.DateTime:
67 return XmlConvert.ToString ((DateTime) o, XmlDateTimeSerializationMode.RoundtripKind);
69 return ((double) o).ToString ("r");
71 return ((float) o).ToString ("r");
72 case TypeCode.Boolean:
73 // Valid XML values are `true' and `false', not `True' and `False' that boolean returns
74 return o.ToString().ToLower();
77 return XmlConvert.ToString ((TimeSpan) o);
78 if (o is DateTimeOffset)
79 return XmlConvert.ToString ((DateTimeOffset) o);
84 public static bool ToBoolean (object o)
86 throw new NotImplementedException ();
89 public static Nullable <bool> ToNullableBoolean (object o)
91 throw new NotImplementedException ();
94 public static IEnumerable ExpandArray (object o)
101 else if (o is IEnumerable)
102 foreach (object obj in (IEnumerable) o)
103 foreach (object oo in ExpandArray (obj))
109 public static XNode ToNode (object o)
112 throw new ArgumentException ("Attribute node is not allowed as argument");
113 XNode n = o as XNode;
116 else if (o is string)
117 return new XText ((string) o);
119 return new XText (ToString (o));
122 public static object GetDetachedObject (XObject child)
124 return child.Owner != null ? Clone (child) : child;
127 public static object Clone (object o)
132 return new XAttribute ((XAttribute) o);
134 return new XElement ((XElement) o);
136 return new XCData ((XCData) o);
138 return new XComment ((XComment) o);
140 return new XPI ((XPI) o);
141 if (o is XDeclaration)
142 return new XDeclaration ((XDeclaration) o);
143 if (o is XDocumentType)
144 return new XDocumentType ((XDocumentType) o);
146 return new XText ((XText) o);
147 throw new ArgumentException ();