Implementing XmlWriter to write XML data

The XmlWriter class is similar to the XmlWriter class provided with the .NET framework. It provides an interface to write data in XML format to an XML destination.

To implement XmlWriter in your Silverlight applications, you first need to create an instance of the XmlWriter class that can write to the XML destination. This is done by calling the XmlWriter.Create() function and passing in the destination location of the XML data.

The XmlWriter.Create() function accepts the destination of the XML data as the first argument and an XmlWriterSettings object as an optional second argument. The source of the XML data can be a Stream object type, TextWriter object, or a StringBuilder object.

For example, the following code creates an XmlWriter object that writes XML data to a StringBuilder object:

StringBuilder xmlData = new StringBuilder(); XmlWriter writer = XmlWriter.Create(xmlData);

The XmlWriterSettings argument provides you with the ability to set specific settings when writing to the XML destination location. Table 15.3 lists the properties that can be applied to XmlWriterSettings objects.

TABLE 15.3

Properties of the XmlWriterSettings Class

Properties

Description

CheckCharacters

Boolean. Specifies whether to do character checking.

CloseOutput

Boolean. Specifies whether the XmlWriter should also close the underlying stream or TextWriter when the Close method is called.

ConformanceLevel

Specifies level of conformance with which the XmlWriter complies.

Encoding

Specifies the text encoding to use when writing XML data.

Indent

Boolean. Specifies whether to indent elements.

IndentChars

String. Specifies the character string to use when indenting. This setting is used when the Indent property is set to true.

NewLineChars

String. Specifies the character string to use for line breaks.

NewLineHandling

Specifies whether to normalize line breaks in the output.

NewLineOn Attributes

Boolean. Specifies whether to write attributes on a new line.

OmitXml Declaration

Boolean. Specifies whether to write an XML declaration.

ReadOnly

Specifies whether this XmlWriterSettings is read-only.

The following code implements an XmlWriterSettings object to create an XmlWriter object that implements indentation and line handling:

StringBuilder xmlData = new StringBuilder(); XmlWriterSettings wSettings = new XmlWriterSettings(); wSettings.Indent = true;

wSettings.NewLineHandling = NewLineHandling.Replace; wSettings.NewLineChars = "\r\n";

XmlWriter writer = XmlWriter.Create(xmlData, wSettings);

After the XmlWriter object is created, data can be written to the XML destination. The simplest way to write an element to the XML destination is to use the WriteStartElement() , WriteEndElement() , WriteStartAttribute(),WriteEndAttribute(), and WriteString() functions. The WriteStartElement() and WriteEndElement() functions write the start and end tags for an XML element. The WriteStartAttribute() and WriteEndAttribute() functions write the start and end tags for an XML attribute of an element. The WriteString() function writes the text value of the XML element.

For example, the following code writes an XML element with the syntax <UserName type="Home">BDayley</User>:

writer.WriteStartElement("UserName");

writer.WriteStartAttribute("Type");

writer.WriteEndElement();

writer.WriteString("Brad");

writer.WriteEndElement();

The data will not actually be written to the XML destination until you call the Flush() or Close() function of the XmlWriter object.

I CRO'sS-R' EF There are numerous XmlWriter functions that can be implemented to write XML H - ■ ; files. For a list of those functions, refer to the MSDN documentation at http://

msdn.microsoft.com/en-us/library/system.xml.xmlwriter_members(VS.95).aspx.

+2 -1

Average user rating: 3.7 stars out of 3 votes

Post a comment

  • Receive news updates via email from this site