public void WriteWhitespace()
 {
     w.WriteStartElement("root");
     w.WriteWhitespace("\t  \n\r");
     w.Close();
     Assert.AreEqual(@"""\t  \n\r""", ResultString);
 }
 /// <summary>
 /// Writes out the given white space.
 /// </summary>
 /// <param name="ws">The string of white space characters.</param>
 public override void WriteWhitespace(string ws)
 {
     _innerWriter.WriteWhitespace(ws);
     if (_tracingWriter != null)
     {
         _tracingWriter.WriteWhitespace(ws);
     }
 }
        public void UseCase1()
        {
            Console.WriteLine();

            MemoryStream        ms = new MemoryStream();
            XmlDictionaryWriter w  = XmlDictionaryWriter.CreateBinaryWriter(ms, null, null);

            w.WriteStartDocument(true);
            w.WriteStartElement("root");
            w.WriteAttributeString("a", "");
            w.WriteComment("");

            w.WriteWhitespace("     ");
            w.WriteStartElement("AAA", "urn:AAA");
            w.WriteEndElement();
            w.WriteStartElement("ePfix", "AAA", "urn:AAABBB");
            w.WriteEndElement();
            w.WriteStartElement("AAA");
            w.WriteCData("CCC\u3005\u4E00CCC");
            w.WriteString("AAA&AAA");
            w.WriteRaw("DDD&DDD");
            w.WriteCharEntity('\u4E01');
            w.WriteComment("COMMENT");
            w.WriteEndElement();
            w.WriteStartElement("AAA");
            w.WriteAttributeString("BBB", "bbb");
            // mhm, how namespace URIs are serialized then?
            w.WriteAttributeString("pfix", "BBB", "urn:bbb", "bbbbb");
            // 0x26-0x3F
            w.WriteAttributeString("CCC", "urn:ccc", "ccccc");
            w.WriteAttributeString("DDD", "urn:ddd", "ddddd");
            w.WriteAttributeString("CCC", "urn:ddd", "cdcdc");

            // XmlLang
            w.WriteXmlAttribute("lang", "ja");
            Assert.AreEqual("ja", w.XmlLang, "XmlLang");

            // XmlSpace
            w.WriteStartAttribute("xml", "space", "http://www.w3.org/XML/1998/namespace");
            w.WriteString("pre");
            w.WriteString("serve");
            w.WriteEndAttribute();
            Assert.AreEqual(XmlSpace.Preserve, w.XmlSpace, "XmlSpace");

            w.WriteAttributeString("xml", "base", "http://www.w3.org/XML/1998/namespace", "local:hogehoge");

            w.WriteString("CCC");
            w.WriteBase64(new byte [] { 0x20, 0x20, 0x20, 0xFF, 0x80, 0x30 }, 0, 6);
            w.WriteEndElement();
            // this WriteEndElement() should result in one more
            // 0x3C, but .net does not output it.
            w.WriteEndElement();
            w.WriteEndDocument();

            w.Close();

            Assert.AreEqual(usecase1_result, ms.ToArray());
        }
        public void WriteQualifiedNameNonDeclaredAttribute()
        {
            xw.WriteStartElement("foo");
            xw.WriteStartAttribute("a", "");
            xw.WriteQualifiedName("attr", "urn:a");
            xw.WriteWhitespace(" ");
            xw.WriteQualifiedName("attr", "urn:b");
            xw.WriteEndAttribute();
            xw.WriteEndElement();
            string xml = sw.ToString();

            Assert.IsTrue(
                xml.IndexOf("<foo ") >= 0,
                "foo");
            Assert.IsTrue(
                xml.IndexOf("a='d1p1:attr d1p2:attr'") > 0,
                "qnames");
            Assert.IsTrue(
                xml.IndexOf(" xmlns:d1p1='urn:a'") > 0,
                "xmlns:a");
            Assert.IsTrue(
                xml.IndexOf(" xmlns:d1p2='urn:b'") > 0,
                "xmlns:b");
        }
 public override void WriteWhitespace(string ws)
 {
     _innerWriter.WriteWhitespace(ws);
 }
Example #6
0
            public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
            {
                if (writer == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));
                }
                if (!MoveToFirst())
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlTokenBufferIsEmpty)));
                }
                int  depth         = 0;
                int  recordedDepth = -1;
                bool include       = true;

                do
                {
                    switch (this.NodeType)
                    {
                    case XmlNodeType.Element:
                        bool isEmpty = this.IsEmptyElement;
                        depth++;
                        if (include &&
                            (null == excludedElementDepth || excludedElementDepth == (depth - 1)) &&
                            this.LocalName == this.excludedElement &&
                            this.NamespaceUri == this.excludedElementNamespace)
                        {
                            include       = false;
                            recordedDepth = depth;
                        }
                        if (include)
                        {
                            writer.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceUri);
                        }
                        if (MoveToFirstAttribute())
                        {
                            do
                            {
                                if (include)
                                {
                                    writer.WriteAttributeString(this.Prefix, this.LocalName, this.NamespaceUri, this.Value);
                                }
                            }while (MoveToNextAttribute());
                        }
                        if (isEmpty)
                        {
                            goto case XmlNodeType.EndElement;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (include)
                        {
                            writer.WriteEndElement();
                        }
                        else if (recordedDepth == depth)
                        {
                            include       = true;
                            recordedDepth = -1;
                        }
                        depth--;
                        break;

                    case XmlNodeType.CDATA:
                        if (include)
                        {
                            writer.WriteCData(this.Value);
                        }
                        break;

                    case XmlNodeType.Comment:
                        if (include)
                        {
                            writer.WriteComment(this.Value);
                        }
                        break;

                    case XmlNodeType.Text:
                        if (include)
                        {
                            writer.WriteString(this.Value);
                        }
                        break;

                    case XmlNodeType.SignificantWhitespace:
                    case XmlNodeType.Whitespace:
                        if (include)
                        {
                            writer.WriteWhitespace(this.Value);
                        }
                        break;

                    case XmlNodeType.DocumentType:
                    case XmlNodeType.XmlDeclaration:
                        break;
                    }
                }while (MoveToNext());
            }
            public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager)
            {
                bool isEmptyElement;

                if (writer == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("writer"));
                }
                if (!this.MoveToFirst())
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.IdentityModel.SR.GetString("XmlTokenBufferIsEmpty")));
                }
                int  num  = 0;
                int  num2 = -1;
                bool flag = true;

Label_0040:
                switch (this.NodeType)
                {
                case XmlNodeType.Element:
                    isEmptyElement = this.IsEmptyElement;
                    num++;
                    if (flag)
                    {
                        if (this.excludedElementDepth.HasValue)
                        {
                            int?excludedElementDepth = this.excludedElementDepth;
                            int num3 = num - 1;
                            if (!((excludedElementDepth.GetValueOrDefault() == num3) && excludedElementDepth.HasValue))
                            {
                                break;
                            }
                        }
                        if ((this.LocalName == this.excludedElement) && (this.NamespaceUri == this.excludedElementNamespace))
                        {
                            flag = false;
                            num2 = num;
                        }
                    }
                    break;

                case XmlNodeType.Text:
                    if (flag)
                    {
                        writer.WriteString(this.Value);
                    }
                    goto Label_01AD;

                case XmlNodeType.CDATA:
                    if (flag)
                    {
                        writer.WriteCData(this.Value);
                    }
                    goto Label_01AD;

                case XmlNodeType.Comment:
                    if (flag)
                    {
                        writer.WriteComment(this.Value);
                    }
                    goto Label_01AD;

                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    if (flag)
                    {
                        writer.WriteWhitespace(this.Value);
                    }
                    goto Label_01AD;

                case XmlNodeType.EndElement:
                    goto Label_0152;

                default:
                    goto Label_01AD;
                }
                if (flag)
                {
                    writer.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceUri);
                }
                if (this.MoveToFirstAttribute())
                {
                    do
                    {
                        if (flag)
                        {
                            writer.WriteAttributeString(this.Prefix, this.LocalName, this.NamespaceUri, this.Value);
                        }
                    }while (this.MoveToNextAttribute());
                }
                if (!isEmptyElement)
                {
                    goto Label_01AD;
                }
Label_0152:
                if (flag)
                {
                    writer.WriteEndElement();
                }
                else if (num2 == num)
                {
                    flag = true;
                    num2 = -1;
                }
                num--;
Label_01AD:
                if (this.MoveToNext())
                {
                    goto Label_0040;
                }
            }
Example #8
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;
            }
        }