XmlWriter.WriteAttributes(XmlReader, Boolean) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
When overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.
public:
virtual void WriteAttributes(System::Xml::XmlReader ^ reader, bool defattr);
public virtual void WriteAttributes (System.Xml.XmlReader reader, bool defattr);
abstract member WriteAttributes : System.Xml.XmlReader * bool -> unit
override this.WriteAttributes : System.Xml.XmlReader * bool -> unit
Public Overridable Sub WriteAttributes (reader As XmlReader, defattr As Boolean)
- reader
- XmlReader
The XmlReader
from which to copy the attributes.
- defattr
- Boolean
true
to copy the default attributes from the XmlReader
; otherwise, false
.
reader
is null
.
The reader is not positioned on an element
, attribute
or XmlDeclaration
node.
An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."
The following example copies all the elements to the output, changes the tag names to upper case, and copies all the attributes unchanged.
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
void main()
{
XmlReader^ reader = XmlReader::Create( L"test1.xml" );
XmlWriterSettings^ settings = gcnew XmlWriterSettings;
settings->Indent = true;
XmlWriter^ writer = XmlWriter::Create( Console::Out );
while ( reader->Read() )
{
if ( reader->NodeType == XmlNodeType::Element )
{
writer->WriteStartElement( reader->Name->ToUpper() );
writer->WriteAttributes( reader, false );
if ( reader->IsEmptyElement )
writer->WriteEndElement();
}
else
if ( reader->NodeType == XmlNodeType::EndElement )
{
writer->WriteEndElement();
}
}
writer->Close();
reader->Close();
}
using System;
using System.IO;
using System.Xml;
public class Sample {
public static void Main() {
XmlReader reader = XmlReader.Create("test1.xml");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(Console.Out);
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
writer.WriteStartElement(reader.Name.ToUpper());
writer.WriteAttributes(reader, false);
if (reader.IsEmptyElement) writer.WriteEndElement();
}
else if (reader.NodeType == XmlNodeType.EndElement) {
writer.WriteEndElement();
}
}
writer.Close();
reader.Close();
}
}
Option Strict
Option Explicit
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
End Class
The example uses the file, test1.xml
, as input.
<test a="1" b="2">
<item c="3" d="4" e="5" f="6"/>
</test>
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.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: