WriteAttributes Method
Collapse the table of content
Expand the table of content

XmlWriter.WriteAttributes Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft