Represents a writer that provides a fast, non-cached, forward-only means of generating streams or files containing XML data.
Namespace:
System.Xml
Assembly:
System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
Public MustInherit Class XmlWriter _
Implements IDisposable
Dim instance As XmlWriter
public abstract class XmlWriter : IDisposable
The XmlWriter class supports the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML recommendations.
Note: |
|---|
You must use the Create method to create XmlWriter object. |
Note When you use the XmlWriter methods to output XML, the elements and attributes will not be written until you call the Close method.
Security Considerations
The following items are things to consider when working with the XmlWriter class.
Exceptions thrown by the XmlWriter can disclose path information that you do not want bubbled up to the application. Your applications must catch exceptions and process them appropriately.
The XmlWriter does not validate any data that is passed to the WriteDocType or WriteRaw methods. You should not pass arbitrary data to these methods.
The following example navigates through the stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.
Dim output As StringBuilder = New StringBuilder()
Dim xmlString As String = "<?xml version='1.0'?>" & _
"<!-- This is a sample XML document -->" & _
"<Items>" & _
"<Item>test with a child element <more/> stuff</Item>" & _
"</Items>"
' Create an XmlReader
Using reader As XmlReader = XmlReader.Create(New StringReader(xmlString))
Dim ws As XmlWriterSettings = New XmlWriterSettings()
ws.Indent = True
Using writer As XmlWriter = XmlWriter.Create(output, ws)
' Parse the file and display each of the nodes.
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(reader.Name)
Case XmlNodeType.Text
writer.WriteString(reader.Value)
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(reader.Name, reader.Value)
Case XmlNodeType.Comment
writer.WriteComment(reader.Value)
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
End Select
End While
End Using
End Using
OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
String xmlString =
@"<?xml version='1.0'?>
<!-- This is a sample XML document -->
<Items>
<Item>test with a child element <more/> stuff</Item>
</Items>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
using (XmlWriter writer = XmlWriter.Create(output, ws))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
}
}
OutputTextBlock.Text = output.ToString();
System..::.Object
System.Xml..::.XmlWriter
System.Xml..::.XmlDictionaryWriter
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources