Export (0) Print
Expand All

XmlWriter.WriteAttributes Method

When overridden in a derived class, writes out all the attributes found at the current position in the XmlReader.

[Visual Basic]
Public Overridable Sub WriteAttributes( _
   ByVal reader As XmlReader, _
   ByVal defattr As Boolean _
)
[C#]
public virtual void WriteAttributes(
 XmlReader reader,
 bool defattr
);
[C++]
public: virtual void WriteAttributes(
 XmlReader* reader,
 bool defattr
);
[JScript]
public function WriteAttributes(
   reader : XmlReader,
 defattr : Boolean
);

Parameters

reader
The XmlReader from which to copy the attributes.
defattr
true to copy the default attributes from the XmlReader; otherwise, false.

Exceptions

Exception Type Condition
ArgumentException reader is a null reference (Nothing in Visual Basic).
XmlException The reader is not positioned on an element, attribute or XmlDeclaration node.

Remarks

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 has no operation.

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 >.

Example

[Visual Basic, C#, C++] The following example copies all the elements to the output, changes the tag names to upper case, and copies all the attributes unchanged.

[Visual Basic] 
Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As New XmlTextReader("test1.xml")
        Dim writer As New XmlTextWriter(Console.Out)
        writer.Formatting = Formatting.Indented
        
        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

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = new XmlTextReader("test1.xml");
    XmlTextWriter writer = new XmlTextWriter(Console.Out);
    writer.Formatting = Formatting.Indented;

    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();
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlTextReader* reader = new XmlTextReader(S"test1.xml");
   XmlTextWriter* writer = new XmlTextWriter(Console::Out);
   writer->Formatting = Formatting::Indented;

   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();
}

The example uses the file, test1.xml, as input.

<test a="1" b="2">
  <item c="3" d="4" e="5" f="6"/>
</test>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlWriter Class | XmlWriter Members | System.Xml Namespace | EntityHandling

Show:
© 2014 Microsoft