public void FullEndElement()
        {
            xw.WriteStartElement("foo");
            xw.WriteFullEndElement();
            Assert.AreEqual("<foo></foo>", Output);

            xw.WriteStartElement("bar");
            xw.WriteAttributeString("foo", "bar");
            xw.WriteFullEndElement();
            Assert.AreEqual("<foo></foo><bar foo='bar'></bar>", Output);

            xw.WriteStartElement("baz");
            xw.WriteStartAttribute("bar", null);
            xw.WriteFullEndElement();
            Assert.AreEqual("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", Output);
        }
 /// <summary>
 /// Closes one element and pops the corresponding namespace scope.
 /// </summary>
 public override void WriteFullEndElement()
 {
     _innerWriter.WriteFullEndElement();
     if (_tracingWriter != null)
     {
         _tracingWriter.WriteFullEndElement();
     }
 }
 public override void WriteFullEndElement()
 {
     _innerWriter.WriteFullEndElement();
 }
Exemple #4
0
        public static async Task WriteNodeAsync(XmlDictionaryWriter writer, XmlReader reader, bool defattr)
        {
            char[]    writeNodeBuffer     = null;
            const int WriteNodeBufferSize = 1024;

            if (null == reader)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            bool canReadChunk = reader.CanReadValueChunk;
            int  d            = reader.NodeType == XmlNodeType.None ? -1 : reader.Depth;

            do
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    writer.WriteAttributes(reader, defattr);
                    if (reader.IsEmptyElement)
                    {
                        writer.WriteEndElement();
                        break;
                    }
                    break;

                case XmlNodeType.Text:
                    if (canReadChunk)
                    {
                        if (writeNodeBuffer == null)
                        {
                            writeNodeBuffer = new char[WriteNodeBufferSize];
                        }
                        int read;
                        while ((read = reader.ReadValueChunk(writeNodeBuffer, 0, WriteNodeBufferSize)) > 0)
                        {
                            writer.WriteChars(writeNodeBuffer, 0, read);
                        }
                    }
                    else
                    {
                        writer.WriteString(await reader.GetValueAsync());
                    }
                    break;

                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    writer.WriteWhitespace(await reader.GetValueAsync());
                    break;

                case XmlNodeType.CDATA:
                    writer.WriteCData(await reader.GetValueAsync());
                    break;

                case XmlNodeType.EntityReference:
                    writer.WriteEntityRef(reader.Name);
                    break;

                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, await reader.GetValueAsync());
                    break;

                case XmlNodeType.DocumentType:
                    writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), await reader.GetValueAsync());
                    break;

                case XmlNodeType.Comment:
                    writer.WriteComment(await reader.GetValueAsync());
                    break;

                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
                }
            } while (await reader.ReadAsync() && (d < reader.Depth || (d == reader.Depth && reader.NodeType == XmlNodeType.EndElement)));
        }
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            try
            {
                do
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        for (int i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);
                            writer.WriteAttributeString(reader.LocalName, reader.NamespaceURI, reader.Value.Replace(sourcePattern, targetPattern));
                        }
                        reader.MoveToElement();
                        if (reader.IsEmptyElement)
                        {
                            writer.WriteEndElement();
                        }
                        break;

                    case XmlNodeType.Text:
                        writer.WriteString(reader.Value.Replace(sourcePattern, targetPattern));
                        break;

                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        writer.WriteWhitespace(reader.Value);
                        break;

                    case XmlNodeType.CDATA:
                        writer.WriteCData(reader.Value);
                        break;

                    case XmlNodeType.EntityReference:
                        writer.WriteEntityRef(reader.Name);
                        break;

                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        writer.WriteProcessingInstruction(reader.Name, reader.Value);
                        break;

                    case XmlNodeType.DocumentType:
                        writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                        break;

                    case XmlNodeType.Comment:
                        writer.WriteComment(reader.Value);
                        break;

                    case XmlNodeType.EndElement:
                        writer.WriteFullEndElement();
                        break;
                    }
                }while (reader.Read());
                writer.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }