/// <summary> /// Transform: read from an input stream and write to an output stream. /// </summary> public void Transform(Stream inputStream, Stream outputStream) { var document = new XmlDocument(); document.PreserveWhitespace = true; document.Load(inputStream); foreach (XmlNode node in document.SelectNodes("/root/data/value")) { var child = node.FirstChild; if (child != null && child.NodeType == XmlNodeType.Text) { var original = child.Value; var args = new TransformStringEventArgs { Value = original }; OnTransformString(args); if (args.Value != original) { child.Value = args.Value; } } } using (var xmlWriter = XmlWriter.Create(outputStream)) { document.WriteTo(xmlWriter); } }
private void OnTransformString(TransformStringEventArgs args) { var handler = TransformString; if (handler != null) { handler(this, args); } }
/// <summary> /// Applies a transformation to the specified string value. /// </summary> /// <param name="value">The string to transform.</param> /// <returns> /// The transformation of <paramref name="value"/>. /// </returns> protected virtual string Transform(string value) { var args = new TransformStringEventArgs { Value = value }; OnTransformString(args); return(args.Value); }
/// <summary> /// Raises the <see cref="TransformString"/> event. /// </summary> /// <param name="args">The event arguments.</param> protected virtual void OnTransformString(TransformStringEventArgs args) => TransformString?.Invoke(this, args);
/// <summary> /// Applies a string transformation to the specified value. /// </summary> /// <param name="transformer">The <see cref="ITransformer"/> to use.</param> /// <param name="args">The <see cref="TransformStringEventArgs"/> to apply the transform to.</param> public static void Apply(this ITransformer transformer, TransformStringEventArgs args) => args.Value = transformer.Transform(args.Value);