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.
Assembly: System.Xml (in System.Xml.dll)
'Declaration Public Overridable Sub WriteAttributes ( _ reader As XmlReader, _ defattr As Boolean _ )
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.
| Exception | Condition |
|---|---|
| ArgumentNullException | reader is Nothing. |
| 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.
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()