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
)

Parameters

reader
Type: System.Xml.XmlReader
The XmlReader from which to copy the attributes.
defattr
Type: System.Boolean
true to copy the default attributes from the XmlReader; otherwise, false.

ExceptionCondition
ArgumentNullException

reader is null.

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.



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


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

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

Community Additions

ADD
Show:
© 2014 Microsoft