Compartir a través de


XmlWriter.WriteAttributes Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Overridable Sub WriteAttributes ( _
    reader As XmlReader, _
    defattr As Boolean _
)
public virtual void WriteAttributes(
    XmlReader reader,
    bool defattr
)

Parameters

  • defattr
    Type: System.Boolean
    true to copy the default attributes from the XmlReader; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

reader is nulla 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 throws an XmlException.

Examples

Dim output As New StringBuilder()

Dim xmlString As String = _
    "<bookstore>" & _
      "<book genre='novel' ISBN='10-861003-324'>" & _
        "<title>The Handmaid's Tale</title>" & _
        "<price>19.95</price>" & _
      "</book>" & _
      "<book genre='novel' ISBN='1-861001-57-5'>" & _
        "<title>Pride And Prejudice</title>" & _
        "<price>24.95</price>" & _
      "</book>" & _
    "</bookstore>"

Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))

    Dim settings As New XmlWriterSettings()
    settings.Indent = True
    Using writer As XmlWriter = XmlWriter.Create(output)

        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
    End Using
End Using

OutputTextBlock.Text = output.ToString()

StringBuilder output = new StringBuilder();

String xmlString =
    @"<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>";


using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output))
    {
        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();
            }
        }
    }
}

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.