XmlWriter.WriteAttributes Method (XmlReader, Boolean)
When overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.
Assembly: System.Xml (in System.Xml.dll)
reader is null.
The reader is not positioned on an element, attribute or XmlDeclaration node.
If the reader is positioned on an element node WriteAttributes copies all the contained attributes. If the reader is positioned on an attribute node, this method writes the current attribute, then the rest of the attributes until the element closing tag. If the reader is positioned on an XmlDeclaration node, this method writes all the attributes in the declaration. If the reader is positioned on any other node type this method throws an XmlException.
If this method is called using XmlValidatingReader, to ensure well-formed XML any content (which has been expanded from the entities) that could result in an invalid document is replaced when written. For example, if an attribute includes an > entity that has been expanded, to ensure a well-formed document the expanded > is replaced when written out with >.
For the asynchronous version of this method, see WriteAttributesAsync.
The following example copies all the elements to the output, changes the tag names to upper case, and copies all the attributes unchanged.
Option Strict Option Explicit Imports System Imports System.IO Imports System.Xml Public Class Sample Public Shared Sub Main() Dim reader As XmlReader = XmlReader.Create("test1.xml") Dim settings As XmlWriterSettings = new XmlWriterSettings() settings.Indent = true Dim writer As XmlWriter = XmlWriter.Create(Console.Out) While reader.Read() If reader.NodeType = XmlNodeType.Element Then writer.WriteStartElement(reader.Name.ToUpper()) writer.WriteAttributes(reader, False) If reader.IsEmptyElement Then writer.WriteEndElement() End If Else If reader.NodeType = XmlNodeType.EndElement Then writer.WriteEndElement() End If End If End While writer.Close() reader.Close() End Sub 'Main End Class 'Sample
The example uses the file, test1.xml, as input.
Available since 8
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1