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.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public virtual void WriteAttributes (
	XmlReader reader,
	bool defattr
)
public void WriteAttributes (
	XmlReader reader, 
	boolean defattr
)
public function WriteAttributes (
	reader : XmlReader, 
	defattr : boolean
)
Not applicable.

Parameters

reader

The XmlReader from which to copy the attributes.

defattr

true to copy the default attributes from the XmlReader; otherwise, false.

Exception typeCondition

ArgumentNullException

reader is a null reference (Nothing in Visual Basic).

XmlException

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

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

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

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft