Export (0) Print
Expand All

XmlWriter.WriteNode Method

When overridden in a derived class, copies everything from the reader to the writer and moves the reader to the start of the next sibling.

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

'Declaration
Public Overridable Sub WriteNode ( _
	reader As XmlReader, _
	defattr As Boolean _
)

Parameters

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

ExceptionCondition
ArgumentException

reader contains invalid characters.

ArgumentNullException

reader is Nothing.

The following table shows the supported node types for this method.

NodeType

WriteNode Behavior

None

Writes out all the nodes irrespective of type. That is, the writer consumes the XmlReader and writes out all the nodes read including attributes, processing instructions, comments, and so on.

This situation occurs when the XmlReader is in an initial state. (The XmlReader.ReadState property returns ReaderState.Initial).

Element

Writes out the element node and any attribute nodes.

Attribute

No operation. Use WriteStartAttribute or WriteAttributeString instead.

Text

Writes out the text node.

CDATA

Writes out the CDATA section node.

EntityReference

Writes out the entity reference node.

ProcessingInstruction

Writes out the processing instruction node.

Comment

Writes out the comment node.

DocumentType

Writes out the document type node.

SignificantWhitespace

Writes out the significant white space node.

Whitespace

Writes out the white space node.

EndElement

Writes out the end element tag.

EndEntity

No operation.

XmlDeclaration

Writes out the XML declaration node.

If the reader is in the initial state, this method moves the reader to the end of file. If the reader is already at the end of file or in a closed state, this method is non-operational.

The following C# code copies an entire XML input document to the console:

XmlReader reader = XmlReader.Create(myfile);
XmlWriter writer = XmlWriter.Create(Console.Out);
writer.WriteNode(reader, false);

If you have moved off the root node and are positioned elsewhere in the document the following C# example correctly writes out the nodes.

XmlReader reader = XmlReader.Create(myfile);
reader.Read(); // Read PI
reader.Read(); // Read Comment
reader.Read(); // Read DOCType
XmlWriter writer = XmlWriter.Create(Console.Out);
while (!reader.EOF){
  writer.WriteNode(reader, false);
 }

If the reader is configured to return white space and the writer has is configured to indent output, WriteNode may produce strange output. You will essentially be getting double formatting.


Dim output As New StringBuilder()

Dim xmlString As String = _
    "<root>" & _
      "<stringValue>" & _
         "<!--comment-->" & _
         "<?some pi?>text value of the element." & _
      "</stringValue>" & _
      "<longValue>270000000000001</longValue>" & _
      "<number>0</number>" & _
      "<double>2E10</double>" & _
      "<date>2003-01-08T15:00:00-00:00</date>" & _
    "</root>"

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

    'Move the reader to the first book element.
    reader.MoveToContent()
    reader.Read()

    'Create a writer that outputs to the console.
    Using writer As XmlWriter = XmlWriter.Create(output)

        'Write the start tag.
        writer.WriteStartElement("myBooks")

        'Write the first book.
        writer.WriteNode(reader, False)

        'Skip the second book.
        reader.Skip()

        'Write the last book.
        writer.WriteNode(reader, False)
        writer.WriteEndElement()

        'Close the writer and the reader.
    End Using
End Using
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