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)
return XmlConvert.ToDecimal (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator decimal? (XElement element)
{
if (element == null)
return element.Value == null ? (decimal?) null : XmlConvert.ToDecimal (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator double (XElement element)
{
if (element == null)
return XmlConvert.ToDouble (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator double? (XElement element)
{
if (element == null)
return element.Value == null ? (double?) null : XmlConvert.ToDouble (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator float (XElement element)
{
if (element == null)
return XmlConvert.ToSingle (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator float? (XElement element)
{
if (element == null)
return element.Value == null ? (float?) null : XmlConvert.ToSingle (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator Guid (XElement element)
{
if (element == null)
return XmlConvert.ToGuid (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator Guid? (XElement element)
{
if (element == null)
return element.Value == null ? (Guid?) null : XmlConvert.ToGuid (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator int (XElement element)
{
if (element == null)
return XmlConvert.ToInt32 (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator int? (XElement element)
{
if (element == null)
return element.Value == null ? (int?) null : XmlConvert.ToInt32 (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator long (XElement element)
{
if (element == null)
return XmlConvert.ToInt64 (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator long? (XElement element)
{
if (element == null)
return element.Value == null ? (ulong?) null : XmlConvert.ToUInt64 (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator TimeSpan (XElement element)
{
if (element == null)
return XmlConvert.ToTimeSpan (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator TimeSpan? (XElement element)
{
if (element == null)
return element.Value == null ? (TimeSpan?) null : XmlConvert.ToTimeSpan (element.Value);
}
+ [CLSCompliant (false)]
public static explicit operator string (XElement element)
{
if (element == null)
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
}
}
+#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 ();
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);
}
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);
}
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);
{
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.
}