public void TestWriteToEndElement() { var xdoc = XDocument.Parse("<!-- test --><test><child></child></test>"); using (var stream = new MemoryStream()) { var writer = XmlWriter.Create(stream); XmlBuilderTool.WriteToEndElement(xdoc.CreateReader(), writer, "child"); writer.Flush(); var writting = System.Text.ASCIIEncoding.UTF8.GetString(stream.ToArray()); Assert.IsTrue(writting == "<?xml version=\"1.0\" encoding=\"utf-8\"?><!-- test --><test><child", writting); } }
public void TestReadAndWriteToNextStartOrEndElement() { var xdoc = XDocument.Parse("<!-- test --><test><child></child></test>"); using (var stream = new MemoryStream()) { var writer = XmlWriter.Create(stream); var reader = xdoc.CreateReader(); XmlBuilderTool.ReadAndWriteToNextStartOrEndElement(reader, writer); writer.Flush(); var writting = System.Text.ASCIIEncoding.UTF8.GetString(stream.ToArray()); Assert.IsTrue(writting == "<?xml version=\"1.0\" encoding=\"utf-8\"?><!-- test -->", writting); Assert.IsTrue(reader.NodeType == XmlNodeType.Element); Assert.IsTrue(reader.LocalName == "test"); } }
/// <summary> /// Updates the specified stream origin On stream destination. /// </summary> /// <param name="streamOrigin"> /// The stream origin. /// </param> /// <param name="streamDestination"> /// The stream destination. /// </param> public void Update(Stream streamOrigin, Stream streamDestination) { if (streamOrigin == null) { throw new ArgumentNullException(nameof(streamOrigin)); } if (streamDestination == null) { throw new ArgumentNullException(nameof(streamDestination)); } streamOrigin.Seek(0, SeekOrigin.Begin); using (var reader = XmlReader.Create(streamOrigin)) { using (var writer = XmlWriter.Create(streamDestination, new XmlWriterSettings { Indent = true, })) { writer.WriteStartDocument(); XmlBuilderTool.ReadAndWriteToNextStartOrEndElement(reader, writer); this.WriteStartRoot(writer); XmlBuilderTool.ReadAndWriteToNextStartOrEndElement(reader, writer); if (reader.EOF || reader.NodeType == XmlNodeType.EndElement) { this.WriteChildren(writer); } else { this.UpdateChildren(reader, writer); } writer.WriteEndElement(); writer.WriteEndDocument(); } } }
/// <summary> /// Updates the specified reader. /// </summary> /// <param name="reader"> /// The reader. /// </param> /// <param name="writer"> /// The writer. /// </param> public void Update(XmlReader reader, XmlWriter writer) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } this.WriteStartElement(writer); XmlBuilderTool.ReadAndWriteToNextStartOrEndElement(reader, writer); if (reader.EOF || reader.NodeType == XmlNodeType.EndElement) { this.WriteChildren(writer); } else { this.UpdateChildren(reader, writer); } writer.WriteEndElement(); }