November 2003
Intermediate to advanced
476 pages
14h 38m
English
Using XmlTextWriter is
very simple, if you want to write your XML in standard angle-brackets
syntax. But since you learned how to read PYX in Chapter 3, you should learn how to write PYX here.
Because PYX does not handle many XML structural features, the
implementation is quite simple. Example 4-6 shows a
possible implementation of an XmlPyxWriter. After
you look over the code, I’ll highlight some
important bits.
using System; using System.Collections; using System.Globalization; using System.IO; using System.Xml; public class XmlPyxWriter : XmlWriter { // constructors public XmlPyxWriter(TextWriter writer) { this.writer = writer; } public XmlPyxWriter(Stream stream) { this.writer = new StreamWriter(stream); } public XmlPyxWriter(string filename) { this.writer = new StreamWriter(filename); } // private instance variables private TextWriter writer; private WriteState writeState = WriteState.Start; private XmlSpace xmlSpace = XmlSpace.Default; private string xmlLang = CultureInfo.CurrentCulture.ThreeLetterISOLanguageName; private Stack elementNames = new Stack( ); // private instance methods private void Write(string text) { writer.WriteLine("-{0}", text); if (writeState == WriteState.Element) { writeState = WriteState.Content; } } private void Write(char ch) { writer.WriteLine("-{0}", ch); if (writeState == WriteState.Element) { writeState = WriteState.Content; } } private void Write(char [ ] buffer, int index, int count) ...Read now
Unlock full access