WriteAttributes() public method

public WriteAttributes ( XmlReader reader, bool defattr ) : void
reader XmlReader
defattr bool
return void
コード例 #1
0
 public virtual void WriteXml(System.Xml.XmlWriter writer)
 {
     writer.WriteStartElement("WriteAttributes");
     using (StringReader sr = new StringReader(CommonUtilities.XmlStringForAttributes))
     {
         using (XmlReader reader = new XmlTextReader(sr))
         {
             bool found = false;
             while (reader.Read())
             {
                 if (reader.Name.Equals("attributeHolder"))
                 {
                     found = true;
                     break;
                 }
             }
             if (found == false)
             {
                 throw new Exception("could not find the attributeHolder node");
             }
             // reader.ReadElementString("attributeHolder"); //moves to the root node
             writer.WriteAttributes(reader, false);
         }
     }
     writer.WriteEndElement();
 }
コード例 #2
0
        private static void WriteShallowNode(XmlReader reader, XmlWriter writer)
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    writer.WriteAttributes(reader, true);
                    if (reader.IsEmptyElement)
                    {
                        writer.WriteEndElement();
                    }
                    return;

                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    return;

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

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

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

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

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

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

                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    return;

                case XmlNodeType.XmlDeclaration:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    return;
            }
            throw new InvalidOperationException("Invalid node");
        }
コード例 #3
0
 private static void CopyUntilEndOfChanges(XmlReader reader, XmlWriter writer)
 {
     Debug.Assert(writer != null);
     Debug.Assert(reader != null);
     reader.MoveToContent();
     while (!(reader.EOF || reader.NodeType == XmlNodeType.EndElement && reader.Name == "Changes"))
     {
         if (reader.NodeType == XmlNodeType.Element)
         {
             writer.WriteStartElement(reader.Name);
             writer.WriteAttributes(reader, false);
         }
         else if (reader.NodeType == XmlNodeType.Text)
         {
             writer.WriteString(reader.Value);
         }
         else if (reader.NodeType == XmlNodeType.EndElement)
         {
             writer.WriteEndElement();
         }
         reader.Read();
     }
 }
コード例 #4
0
 // Writes the content (inner XML) of the current node into the provided XmlWriter.
 private async Task WriteNodeAsync(XmlWriter xtw, bool defattr)
 {
     int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
     while (await this.ReadAsync().ConfigureAwait(false) && (d < this.Depth))
     {
         switch (this.NodeType)
         {
             case XmlNodeType.Element:
                 xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
                 xtw.WriteAttributes(this, defattr);
                 if (this.IsEmptyElement)
                 {
                     xtw.WriteEndElement();
                 }
                 break;
             case XmlNodeType.Text:
                 xtw.WriteString(await this.GetValueAsync().ConfigureAwait(false));
                 break;
             case XmlNodeType.Whitespace:
             case XmlNodeType.SignificantWhitespace:
                 xtw.WriteWhitespace(await this.GetValueAsync().ConfigureAwait(false));
                 break;
             case XmlNodeType.CDATA:
                 xtw.WriteCData(this.Value);
                 break;
             case XmlNodeType.EntityReference:
                 xtw.WriteEntityRef(this.Name);
                 break;
             case XmlNodeType.XmlDeclaration:
             case XmlNodeType.ProcessingInstruction:
                 xtw.WriteProcessingInstruction(this.Name, this.Value);
                 break;
             case XmlNodeType.DocumentType:
                 xtw.WriteDocType(this.Name, this.GetAttribute("PUBLIC"), this.GetAttribute("SYSTEM"), this.Value);
                 break;
             case XmlNodeType.Comment:
                 xtw.WriteComment(this.Value);
                 break;
             case XmlNodeType.EndElement:
                 xtw.WriteFullEndElement();
                 break;
         }
     }
     if (d == this.Depth && this.NodeType == XmlNodeType.EndElement)
     {
         await ReadAsync().ConfigureAwait(false);
     }
 }
コード例 #5
0
ファイル: Business.cs プロジェクト: ErikCH/XMLAssignment
        //copies from example
        private void PassNodeThrough(XmlReader reader, XmlWriter writer)
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    writer.WriteAttributes(reader, true);
                    if (reader.IsEmptyElement)
                    {
                        writer.WriteEndElement();
                    }
                    break;

                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    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;
            }
        }
コード例 #6
0
        // Writes the content (inner XML) of the current node into the provided XmlWriter.
        private void WriteNode(XmlWriter xtw, bool defattr) {
#if !SILVERLIGHT
            Debug.Assert(xtw is XmlTextWriter);
#endif
            int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
            while (this.Read() && (d < this.Depth)) {
                switch (this.NodeType) {
                    case XmlNodeType.Element:
                        xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
#if !SILVERLIGHT // Removing dependency on XmlTextWriter
                        ((XmlTextWriter)xtw).QuoteChar = this.QuoteChar;
#endif
                        xtw.WriteAttributes(this, defattr);
                        if (this.IsEmptyElement) {
                            xtw.WriteEndElement();
                        }
                        break;
                    case XmlNodeType.Text:
                        xtw.WriteString(this.Value);
                        break;
                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        xtw.WriteWhitespace(this.Value);
                        break;
                    case XmlNodeType.CDATA:
                        xtw.WriteCData(this.Value);
                        break;
                    case XmlNodeType.EntityReference:
                        xtw.WriteEntityRef(this.Name);
                        break;
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        xtw.WriteProcessingInstruction(this.Name, this.Value);
                        break;
                    case XmlNodeType.DocumentType:
                        xtw.WriteDocType(this.Name, this.GetAttribute("PUBLIC"), this.GetAttribute("SYSTEM"), this.Value);
                        break;
                    case XmlNodeType.Comment:
                        xtw.WriteComment(this.Value);
                        break;
                    case XmlNodeType.EndElement:
                        xtw.WriteFullEndElement();
                        break;
                }
            }
            if (d == this.Depth && this.NodeType == XmlNodeType.EndElement) {
                Read();
            }
        }
コード例 #7
0
ファイル: Client.cs プロジェクト: baulig/Provcon-Faust
        // From http://blogs.msdn.com/b/mfussell/archive/2005/02/12/371546.aspx
        static void WriteShallowNode( XmlReader reader, XmlWriter writer )
        {
            if ( reader == null )
            {
                throw new ArgumentNullException("reader");
            }
            if ( writer == null )
            {
                throw new ArgumentNullException("writer");
            }

            switch ( reader.NodeType )
            {
            case XmlNodeType.Element:
                writer.WriteStartElement( reader.Prefix, reader.LocalName, reader.NamespaceURI );
                writer.WriteAttributes( reader, true );
                if ( reader.IsEmptyElement )
                {
                    writer.WriteEndElement();
                }
                break;
            case XmlNodeType.Text:
                writer.WriteString( reader.Value );
                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;
            }
        }
コード例 #8
0
 public override void WriteAttributes(XmlReader reader, bool defattr)
 {
     CheckAsync();
     _coreWriter.WriteAttributes(reader, defattr);
 }
コード例 #9
0
        /// <summary>
        /// This is used to write out the body content of a topic to the main document part
        /// </summary>
        /// <param name="writer">The writer for the main document part</param>
        /// <param name="reader">The reader for the topic body content</param>
        /// <remarks>Using a reader prevents unnecessary namespaces from appearing on the body content elements
        /// which happens if we convert the XElement content to a string and write it out as raw content.</remarks>
        private static void WriteTopic(XmlWriter writer, XmlReader reader)
        {
            while(!reader.EOF)
            {
                reader.Read();

                switch(reader.NodeType)
                {
                    case XmlNodeType.Element:
                        writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        writer.WriteAttributes(reader, true);

                        if(reader.IsEmptyElement)
                            writer.WriteEndElement();
                        break;

                    case XmlNodeType.Text:
                        writer.WriteString(reader.Value);
                        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.Comment:
                        writer.WriteComment(reader.Value);
                        break;

                    case XmlNodeType.EndElement:
                        // Stop when the end of the body is reached
                        if(reader.LocalName == "body")
                            return;

                        writer.WriteFullEndElement();
                        break;
                }
            }
        }
コード例 #10
0
        private void StreamToInsertionPoint(XmlReader reader, XmlWriter writer)
        {
            if ( reader == null )
            {
                throw new ArgumentNullException("reader");
            }
            if ( writer == null )
            {
                throw new ArgumentNullException("writer");
            }

            while (reader.Read())
            {
                if(reader.LocalName == "notes" && reader.IsEmptyElement)
                {
                    writer.WriteStartElement("notes");
                    writer.WriteAttributes(reader, false);
                    return;
                }
                if(reader.LocalName == "notes" && !reader.IsStartElement())
                {
                    return;
                }
                StreamNode(reader, writer);
            }
        }
コード例 #11
0
ファイル: Utilities.cs プロジェクト: JohnThomson/libpalaso
		private static void ProcessElement(XmlReader reader, XmlWriter writer)
		{
				if (reader.Name == "entry")
				{
					string guid = reader.GetAttribute("guid");
					if (String.IsNullOrEmpty(guid))
					{
						guid = Guid.NewGuid().ToString();
						writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
						writer.WriteAttributes(reader, true);
						writer.WriteAttributeString("guid", guid);
						string s = reader.ReadInnerXml();//this seems to be enough to get the reader to the next element
						writer.WriteRaw(s);
						writer.WriteEndElement();
					}
					else
					{
						writer.WriteNode(reader, true);
					}
				}
				else
				{
					WriteShallowNode(reader, writer);
				}
		}
コード例 #12
0
        protected void CopyNode(XmlReader reader, XmlWriter writer)
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    writer.WriteAttributes(reader, false);

                    if (reader.IsEmptyElement)
                    {
                        writer.WriteEndElement();
                    }
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteEndElement();
                    break;
                case XmlNodeType.Text:
                    writer.WriteValue(reader.Value);
                    break;
            }
        }
コード例 #13
0
        private static void LimitHtmlOnWordBoundary(XmlWriter writer, XmlReader reader, int maxLength, out bool shouldWriteEllipses)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            int elementCount = 0;

            int currentLength = 0;
            shouldWriteEllipses = false;

            int magicMinimumLength = Math.Min(5, (maxLength + 1) / 2);

            int num = (reader.NodeType == XmlNodeType.None) ? -1 : reader.Depth;
            do
            {
                bool done = false;
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        elementCount++;
                        writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        writer.WriteAttributes(reader, false);
                        if (reader.IsEmptyElement)
                        {
                            elementCount--;
                            writer.WriteEndElement();
                        }
                        break;

                    case XmlNodeType.Text:
                        string value = reader.Value;
                        int strLen = value.Length;

                        if (currentLength + strLen > maxLength)
                        {
                            string almost = value.Substring(0, maxLength - currentLength + 1);
                            int lastSpace = almost.LastIndexOf(' ');
                            if (lastSpace < 0)
                            {
                                if (currentLength < magicMinimumLength)
                                {
                                    value = value.Substring(0, maxLength - currentLength);
                                }
                                else
                                {
                                    value = null;
                                }
                            }
                            else if (lastSpace + currentLength < magicMinimumLength)
                            {
                                value = value.Substring(0, maxLength - currentLength);
                            }
                            else
                            {
                                value = value.Substring(0, lastSpace);
                            }
                            shouldWriteEllipses = true;
                            done = true;
                        }
                        if (value != null)
                        {
                            writer.WriteString(value);
                            currentLength += value.Length;
                        }
                        break;

                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        writer.WriteString(reader.Value);
                        currentLength += reader.Value.Length;
                        break;

                    case XmlNodeType.EndElement:
                        elementCount--;
                        writer.WriteFullEndElement();
                        break;

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

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

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

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

                    case XmlNodeType.DocumentType:
                        //writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                        break;
                }
                if (done) break;
            }
            while (reader.Read() && ((num < reader.Depth) || ((num == reader.Depth) && (reader.NodeType == XmlNodeType.EndElement))));

            while (elementCount > 0)
            {
                writer.WriteFullEndElement();
                elementCount--;
            }
        }
コード例 #14
0
ファイル: XmlUtils.cs プロジェクト: wrmsr/xdc
        public static void WriteShallowNode(XmlReader reader, XmlWriter writer)
        {
            //Console.WriteLine("{0}: {1}", reader.NodeType, reader.Name);

            switch(reader.NodeType) {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Prefix	, reader.LocalName, reader.NamespaceURI);
                    writer.WriteAttributes(reader, true);
                    if(reader.IsEmptyElement)
                        writer.WriteEndElement();
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    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;
            }
        }
コード例 #15
0
		private void CopyUntilElement(XmlWriter writer, XmlReader reader, string elementName)
		{
			Debug.Assert(writer != null);
			Debug.Assert(reader != null);
			Debug.Assert(!string.IsNullOrEmpty(elementName));
			if (reader.NodeType == XmlNodeType.None)
			{
				reader.Read();
			}
			while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement
				&& (reader.NodeType != XmlNodeType.Element || LdmlNodeComparer.CompareElementNames(reader.Name, elementName) < 0))
			{
				// XmlWriter.WriteNode doesn't do anything if the node type is Attribute
				if (reader.NodeType == XmlNodeType.Attribute)
				{
					writer.WriteAttributes(reader, false);
				}
				else
				{
					writer.WriteNode(reader, false);
				}
			}
		}
コード例 #16
0
ファイル: CommonTests.cs プロジェクト: johnhhm/corefx
 private void InvokeMethod(XmlWriter w, string methodName)
 {
     byte[] buffer = new byte[10];
     switch (methodName)
     {
         case "WriteStartDocument":
             w.WriteStartDocument();
             break;
         case "WriteStartElement":
             w.WriteStartElement("root");
             break;
         case "WriteEndElement":
             w.WriteEndElement();
             break;
         case "WriteStartAttribute":
             w.WriteStartAttribute("attr");
             break;
         case "WriteEndAttribute":
             w.WriteEndAttribute();
             break;
         case "WriteCData":
             w.WriteCData("test");
             break;
         case "WriteComment":
             w.WriteComment("test");
             break;
         case "WritePI":
             w.WriteProcessingInstruction("name", "test");
             break;
         case "WriteEntityRef":
             w.WriteEntityRef("e");
             break;
         case "WriteCharEntity":
             w.WriteCharEntity('c');
             break;
         case "WriteSurrogateCharEntity":
             w.WriteSurrogateCharEntity('\uDC00', '\uDBFF');
             break;
         case "WriteWhitespace":
             w.WriteWhitespace(" ");
             break;
         case "WriteString":
             w.WriteString("foo");
             break;
         case "WriteChars":
             char[] charArray = new char[] { 'a', 'b', 'c', 'd' };
             w.WriteChars(charArray, 0, 3);
             break;
         case "WriteRaw":
             w.WriteRaw("<foo>bar</foo>");
             break;
         case "WriteBase64":
             w.WriteBase64(buffer, 0, 9);
             break;
         case "WriteBinHex":
             w.WriteBinHex(buffer, 0, 9);
             break;
         case "LookupPrefix":
             string str = w.LookupPrefix("foo");
             break;
         case "WriteNmToken":
             w.WriteNmToken("foo");
             break;
         case "WriteName":
             w.WriteName("foo");
             break;
         case "WriteQualifiedName":
             w.WriteQualifiedName("foo", "bar");
             break;
         case "WriteValue":
             w.WriteValue(Int32.MaxValue);
             break;
         case "WriteAttributes":
             XmlReader xr1 = ReaderHelper.Create(new StringReader("<root attr='test'/>"));
             xr1.Read();
             w.WriteAttributes(xr1, false);
             break;
         case "WriteNodeReader":
             XmlReader xr2 = ReaderHelper.Create(new StringReader("<root/>"));
             xr2.Read();
             w.WriteNode(xr2, false);
             break;
         case "Flush":
             w.Flush();
             break;
         default:
             CError.Equals(false, "Unexpected param in testcase: {0}", methodName);
             break;
     }
 }
コード例 #17
0
		private void CopyToEndElement(XmlWriter writer, XmlReader reader)
		{
			Debug.Assert(writer != null);
			Debug.Assert(reader != null);
			while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement)
			{
				// XmlWriter.WriteNode doesn't do anything if the node type is Attribute
				if (reader.NodeType == XmlNodeType.Attribute)
				{
					writer.WriteAttributes(reader, false);
				}
				else
				{
					writer.WriteNode(reader, false);
				}
			}
			// either read the end element or no-op if EOF
			reader.Read();
		}
コード例 #18
0
 public override void WriteAttributes(XmlReader reader, bool defattr)
 {
     _writer.WriteAttributes(reader, defattr);
 }
コード例 #19
0
        // Writes the content (inner XML) of the current node into the provided XmlWriter.
        private async Task WriteNodeAsync(XmlWriter xtw, bool defattr)
        {
#if !SILVERLIGHT
            Debug.Assert(xtw is XmlTextWriter);
#endif
            int d = this.NodeType == XmlNodeType.None ? -1 : this.Depth;
            while (await this.ReadAsync().ConfigureAwait(false) && (d < this.Depth))
            {
                switch (this.NodeType)
                {
                case XmlNodeType.Element:
                    xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
#if !SILVERLIGHT // Removing dependency on XmlTextWriter
                    ((XmlTextWriter)xtw).QuoteChar = this.QuoteChar;
#endif
                    xtw.WriteAttributes(this, defattr);
                    if (this.IsEmptyElement)
                    {
                        xtw.WriteEndElement();
                    }
                    break;

                case XmlNodeType.Text:
                    xtw.WriteString(await this.GetValueAsync().ConfigureAwait(false));
                    break;

                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    xtw.WriteWhitespace(await this.GetValueAsync().ConfigureAwait(false));
                    break;

                case XmlNodeType.CDATA:
                    xtw.WriteCData(this.Value);
                    break;

                case XmlNodeType.EntityReference:
                    xtw.WriteEntityRef(this.Name);
                    break;

                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    xtw.WriteProcessingInstruction(this.Name, this.Value);
                    break;

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

                case XmlNodeType.Comment:
                    xtw.WriteComment(this.Value);
                    break;

                case XmlNodeType.EndElement:
                    xtw.WriteFullEndElement();
                    break;
                }
            }
            if (d == this.Depth && this.NodeType == XmlNodeType.EndElement)
            {
                await ReadAsync().ConfigureAwait(false);
            }
        }
コード例 #20
0
        private void WriteNode(XmlWriter xtw, bool defattr)
        {
            int num = (this.NodeType == XmlNodeType.None) ? -1 : this.Depth;
            while (this.Read() && (num < this.Depth))
            {
                switch (this.NodeType)
                {
                    case XmlNodeType.Element:
                        xtw.WriteStartElement(this.Prefix, this.LocalName, this.NamespaceURI);
                        ((XmlTextWriter) xtw).QuoteChar = this.QuoteChar;
                        xtw.WriteAttributes(this, defattr);
                        if (this.IsEmptyElement)
                        {
                            xtw.WriteEndElement();
                        }
                        break;

                    case XmlNodeType.Text:
                        xtw.WriteString(this.Value);
                        break;

                    case XmlNodeType.CDATA:
                        xtw.WriteCData(this.Value);
                        break;

                    case XmlNodeType.EntityReference:
                        xtw.WriteEntityRef(this.Name);
                        break;

                    case XmlNodeType.ProcessingInstruction:
                    case XmlNodeType.XmlDeclaration:
                        xtw.WriteProcessingInstruction(this.Name, this.Value);
                        break;

                    case XmlNodeType.Comment:
                        xtw.WriteComment(this.Value);
                        break;

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

                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        xtw.WriteWhitespace(this.Value);
                        break;

                    case XmlNodeType.EndElement:
                        xtw.WriteFullEndElement();
                        break;
                }
            }
            if ((num == this.Depth) && (this.NodeType == XmlNodeType.EndElement))
            {
                this.Read();
            }
        }
コード例 #21
0
        /// <summary>
        /// Preprocesses and copies an Xml document from a reader into a writer.
        /// </summary>
        /// <param name="xmlReader">The Xml reader.</param>
        /// <param name="xmlWriter">The Xml writer.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="xmlReader"/>
        /// or <paramref name="xmlWriter"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the input Xml is malformed
        /// such as if it contains unbalanced ifdef/endif pairs.</exception>
        public void Preprocess(XmlReader xmlReader, XmlWriter xmlWriter)
        {
            if (xmlReader == null)
                throw new ArgumentNullException("xmlReader");
            if (xmlWriter == null)
                throw new ArgumentNullException("xmlWriter");

            // Tracks whether a given block has been included or excluded.
            Stack<bool> blockStack = new Stack<bool>();
            blockStack.Push(true);

            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.ProcessingInstruction)
                {
                    switch (xmlReader.Name)
                    {
                        case "define":
                            if (blockStack.Peek())
                                Define(xmlReader.Value.Trim());
                            continue;

                        case "ifdef":
                            blockStack.Push(blockStack.Peek() && IsDefined(xmlReader.Value.Trim()));
                            continue;

                        case "ifndef":
                            blockStack.Push(blockStack.Peek() && !IsDefined(xmlReader.Value.Trim()));
                            continue;

                        case "else":
                            if (blockStack.Count == 1)
                                throw new InvalidOperationException(
                                    "Found <?else?> instruction without enclosing <?ifdef?> or <?ifndef?> block.");
                            blockStack.Push(!blockStack.Pop() && blockStack.Peek()); // order matters
                            continue;

                        case "endif":
                            if (blockStack.Count == 1)
                                throw new InvalidOperationException(
                                    "Found <?endif?> instruction without matching <?ifdef?> or <?ifndef?>.");
                            blockStack.Pop();
                            continue;
                    }
                }

                if (!blockStack.Peek())
                    continue;

                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element:
                        xmlWriter.WriteStartElement(xmlReader.Prefix, xmlReader.LocalName, xmlReader.NamespaceURI);
                        xmlWriter.WriteAttributes(xmlReader, true);
                        if (xmlReader.IsEmptyElement)
                            xmlWriter.WriteEndElement();
                        break;

                    case XmlNodeType.Text:
                        xmlWriter.WriteValue(xmlReader.Value);
                        break;

                    case XmlNodeType.CDATA:
                        xmlWriter.WriteCData(xmlReader.Value);
                        break;

                    case XmlNodeType.EntityReference:
                        xmlWriter.WriteEntityRef(xmlReader.Name);
                        break;

                    case XmlNodeType.Comment:
                        xmlWriter.WriteComment(xmlReader.Value);
                        break;

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

                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        xmlWriter.WriteWhitespace(xmlReader.Value);
                        break;

                    case XmlNodeType.EndElement:
                        xmlWriter.WriteFullEndElement();
                        break;

                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        xmlWriter.WriteProcessingInstruction(xmlReader.Name, xmlReader.Value);
                        break;
                }
            }

            if (blockStack.Count != 1)
                throw new InvalidOperationException("Missing <?endif?> instruction.");

            xmlWriter.Flush();
        }