Exemplo n.º 1
0
 private static void WriteXmlRootElementCore(XmlWriter writer, object value, Action <XmlWriter, object> treeWriterPublic, Action <XmlWriter, IHierarchy <object> > treeWriterInternal, XmlQualifiedEntity rootEntity = null)
 {
     Validator.ThrowIfNull(writer, nameof(writer));
     Validator.ThrowIfNull(value, nameof(value));
     try
     {
         IHierarchy <object> nodes;
         XmlQualifiedEntity  rootElement;
         if (treeWriterInternal == null)
         {
             nodes       = new Hierarchy <object>().Add(value);
             rootElement = nodes.LookupXmlStartElement(rootEntity);
             writer.WriteStartElement(rootElement.Prefix, rootElement.LocalName, rootElement.Namespace);
             treeWriterPublic?.Invoke(writer, value);
         }
         else
         {
             nodes       = new HierarchySerializer(value).Nodes;
             rootElement = nodes.LookupXmlStartElement(rootEntity);
             writer.WriteStartElement(rootElement.Prefix, rootElement.LocalName, rootElement.Namespace);
             treeWriterInternal(writer, nodes);
         }
     }
     catch (Exception ex)
     {
         Exception innerException = ex;
         if (innerException is OutOfMemoryException)
         {
             throw;
         }
         if (innerException is TargetInvocationException)
         {
             innerException = innerException.InnerException;
         }
         throw ExceptionUtility.Refine(new InvalidOperationException("There is an error in the XML document.", innerException), MethodBaseConverter.FromType(typeof(XmlWriterExtensions), flags: ReflectionUtility.BindingInstancePublicAndPrivateNoneInheritedIncludeStatic), writer, value).Unwrap();
     }
     writer.WriteEndElement();
     writer.Flush();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Converts the specified string to its <typeparamref name="T"/> equivalent using the specified <paramref name="context"/> and <paramref name="culture"/> information.
 /// </summary>
 /// <typeparam name="T">The type of the expected return <paramref name="value"/> after conversion.</typeparam>
 /// <param name="value">The string value to convert.</param>
 /// <param name="culture">The culture-specific formatting information about <paramref name="value"/>.</param>
 /// <param name="context">The type-specific formatting information about <paramref name="value"/>.</param>
 /// <returns>An object that is equivalent to <typeparamref name="T"/> contained in <paramref name="value"/>, as specified by <paramref name="culture"/> and <paramref name="context"/>.</returns>
 /// <exception cref="ArgumentException">
 /// Invalid <paramref name="value"/> for <typeparamref name="T"/> specified.
 /// </exception>
 /// <exception cref="NotSupportedException">
 /// The conversion cannot be performed.
 /// </exception>
 public static T FromString <T>(string value, CultureInfo culture, ITypeDescriptorContext context)
 {
     try
     {
         Type          resultType = typeof(T);
         TypeConverter converter  = TypeDescriptor.GetConverter(resultType);
         T             result     = (T)converter.ConvertFromString(context, culture, value);
         if (resultType == typeof(Uri)) // for reasons unknown to me, MS allows all sorts of string to be constructed on a Uri - check if valid (quick-fix until more knowledge of ITypeDescriptorContext)
         {
             Uri      resultAsUri = result as Uri;
             string[] segments    = resultAsUri?.Segments;
         }
         return(result);
     }
     catch (Exception ex)
     {
         if (ex.GetType() == typeof(NotSupportedException))
         {
             throw;
         }
         throw ExceptionUtility.Refine(ExceptionUtility.CreateArgumentException(nameof(value), ex.Message, ex.InnerException), MethodBaseConverter.FromType(typeof(Converter), EnumerableConverter.AsArray(typeof(string), typeof(CultureInfo), typeof(ITypeDescriptorContext))), value, culture, context).Unwrap();
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Converts an object into its XML representation.
 /// </summary>
 /// <param name="writer">The <see cref="XmlWriter" /> stream to which the object is serialized.</param>
 /// <param name="value">The object to convert.</param>
 /// <exception cref="InvalidOperationException">There is an error in the XML document.</exception>
 public virtual void WriteXml(XmlWriter writer, object value)
 {
     Validator.ThrowIfNull(writer, nameof(writer));
     Validator.ThrowIfNull(value, nameof(value));
     try
     {
         var root        = new HierarchySerializer(value);
         var rootElement = root.Nodes.LookupXmlStartElement(Options.RootName);
         writer.WriteStartElement(rootElement.Prefix, rootElement.LocalName, rootElement.Namespace);
         WriteXmlNodes(writer, root.Nodes);
     }
     catch (Exception ex)
     {
         Exception innerException = ex;
         if (innerException is OutOfMemoryException)
         {
             throw;
         }
         if (innerException is TargetInvocationException)
         {
             innerException = innerException.InnerException;
         }
         throw ExceptionUtility.Refine(new InvalidOperationException("There is an error in the XML document.", innerException), MethodBaseConverter.FromType(typeof(XmlConverter), flags: ReflectionUtility.BindingInstancePublicAndPrivateNoneInheritedIncludeStatic), writer, value).Unwrap();
     }
     writer.WriteEndElement();
     writer.Flush();
 }