New test.
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XElement.cs
index ef6163bc3a5ef789b6d4a3168034381786e7357f..16c969c332aaf5a2d89e334b1327b2b5d45a4dc1 100644 (file)
@@ -79,36 +79,62 @@ namespace System.Xml.Linq
                        Add (source.Contents);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator bool (XElement element)
                {
                        if (element == null)
                                throw new ArgumentNullException ("element");
-                       return XmlConvert.ToBoolean (element.Value);
+                       return XUtil.ConvertToBoolean (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator bool? (XElement element)
                {
                        if (element == null)
                                return null;
                        
-                       return element.Value == null ? (bool?) null : XmlConvert.ToBoolean (element.Value);
+                       return element.Value == null ? (bool?) null : XUtil.ConvertToBoolean (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator DateTime (XElement element)
                {
                        if (element == null)
                                throw new ArgumentNullException ("element");
-                       return XmlConvert.ToDateTime (element.Value, XmlDateTimeSerializationMode.RoundtripKind);
+                       return XUtil.ToDateTime (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator DateTime? (XElement element)
                {
                        if (element == null)
                                return null;
                        
-                       return element.Value == null ? (DateTime?) null : XmlConvert.ToDateTime (element.Value, XmlDateTimeSerializationMode.RoundtripKind);
+                       return element.Value == null ? (DateTime?) null : XUtil.ToDateTime (element.Value);
+               }
+
+#if !TARGET_JVM // Same as for System.Xml.XmlConvert.ToDateTimeOffset
+
+               [CLSCompliant (false)]
+               public static explicit operator DateTimeOffset (XElement element)
+               {
+                       if (element == null)
+                               throw new ArgumentNullException ("element");
+                       return XmlConvert.ToDateTimeOffset (element.Value);
+               }
+
+               [CLSCompliant (false)]
+               public static explicit operator DateTimeOffset? (XElement element)
+               {
+                       if (element == null)
+                               return null;
+                       
+                       return element.Value == null ? (DateTimeOffset?) null : XmlConvert.ToDateTimeOffset (element.Value);
                }
 
+#endif
+
+               [CLSCompliant (false)]
                public static explicit operator decimal (XElement element)
                {
                        if (element == null)
@@ -116,6 +142,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToDecimal (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator decimal? (XElement element)
                {
                        if (element == null)
@@ -124,6 +151,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (decimal?) null : XmlConvert.ToDecimal (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator double (XElement element)
                {
                        if (element == null)
@@ -131,6 +159,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToDouble (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator double? (XElement element)
                {
                        if (element == null)
@@ -139,6 +168,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (double?) null : XmlConvert.ToDouble (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator float (XElement element)
                {
                        if (element == null)
@@ -146,6 +176,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToSingle (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator float? (XElement element)
                {
                        if (element == null)
@@ -154,6 +185,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (float?) null : XmlConvert.ToSingle (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator Guid (XElement element)
                {
                        if (element == null)
@@ -161,6 +193,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToGuid (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator Guid? (XElement element)
                {
                        if (element == null)
@@ -169,6 +202,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (Guid?) null : XmlConvert.ToGuid (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator int (XElement element)
                {
                        if (element == null)
@@ -176,6 +210,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToInt32 (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator int? (XElement element)
                {
                        if (element == null)
@@ -184,6 +219,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (int?) null : XmlConvert.ToInt32 (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator long (XElement element)
                {
                        if (element == null)
@@ -191,6 +227,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToInt64 (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator long? (XElement element)
                {
                        if (element == null)
@@ -233,6 +270,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (ulong?) null : XmlConvert.ToUInt64 (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator TimeSpan (XElement element)
                {
                        if (element == null)
@@ -240,6 +278,7 @@ namespace System.Xml.Linq
                        return XmlConvert.ToTimeSpan (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator TimeSpan? (XElement element)
                {
                        if (element == null)
@@ -248,6 +287,7 @@ namespace System.Xml.Linq
                        return element.Value == null ? (TimeSpan?) null : XmlConvert.ToTimeSpan (element.Value);
                }
 
+               [CLSCompliant (false)]
                public static explicit operator string (XElement element)
                {
                        if (element == null)
@@ -354,7 +394,7 @@ namespace System.Xml.Linq
 
                static void DefineDefaultSettings (XmlReaderSettings settings, LoadOptions options)
                {
-#if NET_2_1
+#if MOONLIGHT
                        // 2.1 has a DtdProcessing property which defaults to DtdProcessing.Prohibit
                        settings.DtdProcessing = DtdProcessing.Parse;
 #else
@@ -414,6 +454,23 @@ namespace System.Xml.Linq
                        }
                }
 
+#if MOONLIGHT || NET_4_0
+               public static XElement Load (Stream stream)
+               {
+                       return Load (stream, LoadOptions.None);
+               }
+
+               public static XElement Load (Stream stream, LoadOptions options)
+               {
+                       XmlReaderSettings s = new XmlReaderSettings ();
+                       DefineDefaultSettings (s, options);
+
+                       using (XmlReader r = XmlReader.Create (stream, s)) {
+                               return LoadCore (r, options);
+                       }
+               }
+#endif
+
                internal static XElement LoadCore (XmlReader r, LoadOptions options)
                {
                        r.MoveToContent ();
@@ -476,7 +533,13 @@ namespace System.Xml.Linq
                public void Save (string filename, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       s.Indent = options != SaveOptions.DisableFormatting;
+
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
+                               s.Indent = true;
+#if NET_4_0 || MOONLIGHT
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+#endif
                        using (XmlWriter w = XmlWriter.Create (filename, s)) {
                                Save (w);
                        }
@@ -490,7 +553,13 @@ namespace System.Xml.Linq
                public void Save (TextWriter tw, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       s.Indent = options != SaveOptions.DisableFormatting;
+                       
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
+                               s.Indent = true;
+#if NET_4_0 || MOONLIGHT
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+#endif
                        using (XmlWriter w = XmlWriter.Create (tw, s)) {
                                Save (w);
                        }
@@ -501,6 +570,25 @@ namespace System.Xml.Linq
                        WriteTo (w);
                }
 
+#if NET_4_0 || MOONLIGHT
+               public void Save (Stream stream)
+               {
+                       Save (stream, SaveOptions.None);
+               }
+
+               public void Save (Stream stream, SaveOptions options)
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
+                               s.Indent = true;
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+
+                       using (var writer = XmlWriter.Create (stream, s)){
+                               Save (writer);
+                       }
+               }
+#endif
                public IEnumerable <XElement> AncestorsAndSelf ()
                {
                        return GetAncestorList (null, true);
@@ -613,7 +701,7 @@ namespace System.Xml.Linq
                {
                        for (XElement el = this; el != null; el = el.Parent)
                                foreach (XAttribute a in el.Attributes ())
-                                       if (a.IsNamespaceDeclaration && a.Name.LocalName == prefix)
+                                       if (a.IsNamespaceDeclaration && (prefix.Length == 0 && a.Name.LocalName == "xmlns" || a.Name.LocalName == prefix))
                                                return XNamespace.Get (a.Value);
                        return XNamespace.None; // nothing is declared.
                }