2 // System.Xml.XmlTextWriter
5 // Kral Ferch <kral_ferch@hotmail.com>
14 public abstract class XmlWriter
18 protected WriteState ws = WriteState.Start;
24 protected XmlWriter () { }
30 public abstract WriteState WriteState { get; }
32 public abstract string XmlLang { get; }
34 public abstract XmlSpace XmlSpace { get; }
40 public abstract void Close ();
42 public abstract void Flush ();
44 public abstract string LookupPrefix (string ns);
46 private void WriteAttribute (XmlReader reader, bool defattr)
48 if (!defattr && reader.IsDefault)
51 WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
52 while (reader.ReadAttributeValue ()) {
53 switch (reader.NodeType) {
54 case XmlNodeType.Text:
55 WriteString (reader.Value);
57 case XmlNodeType.EntityReference:
58 WriteEntityRef (reader.Name);
65 public virtual void WriteAttributes (XmlReader reader, bool defattr)
68 throw new ArgumentException("null XmlReader specified.", "reader");
70 switch (reader.NodeType) {
71 case XmlNodeType.XmlDeclaration:
72 WriteAttributeString ("version", reader ["version"]);
73 if (reader ["encoding"] != null)
74 WriteAttributeString ("encoding", reader ["encoding"]);
75 if (reader ["standalone"] != null)
76 WriteAttributeString ("standalone", reader ["standalone"]);
78 case XmlNodeType.Element:
79 if (reader.MoveToFirstAttribute ())
80 goto case XmlNodeType.Attribute;
82 case XmlNodeType.Attribute:
84 WriteAttribute (reader, defattr);
85 } while (reader.MoveToNextAttribute ());
88 throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
92 public void WriteAttributeString (string localName, string value)
94 WriteAttributeString ("", localName, "", value);
97 public void WriteAttributeString (string localName, string ns, string value)
99 WriteAttributeString ("", localName, ns, value);
102 public void WriteAttributeString (string prefix, string localName, string ns, string value)
104 // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
105 // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
106 if ((prefix == "xmlns") || (prefix == "" && localName == "xmlns"))
108 ns = "http://www.w3.org/2000/xmlns/";
110 WriteStartAttribute (prefix, localName, ns);
112 WriteEndAttribute ();
115 public abstract void WriteBase64 (byte[] buffer, int index, int count);
117 public abstract void WriteBinHex (byte[] buffer, int index, int count);
119 public abstract void WriteCData (string text);
121 public abstract void WriteCharEntity (char ch);
123 public abstract void WriteChars (char[] buffer, int index, int count);
125 public abstract void WriteComment (string text);
127 public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
129 public void WriteElementString (string localName, string value)
131 WriteStartElement(localName);
136 public void WriteElementString (string localName, string ns, string value)
138 WriteStartElement(localName, ns);
143 public abstract void WriteEndAttribute ();
145 public abstract void WriteEndDocument ();
147 public abstract void WriteEndElement ();
149 public abstract void WriteEntityRef (string name);
151 public abstract void WriteFullEndElement ();
153 public abstract void WriteName (string name);
155 public abstract void WriteNmToken (string name);
157 public virtual void WriteNode (XmlReader reader, bool defattr)
160 throw new ArgumentException ();
162 if (reader.ReadState == ReadState.Initial) {
165 WriteNode (reader, defattr);
166 } while (!reader.EOF);
170 switch (reader.NodeType) {
171 case XmlNodeType.Element:
172 WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
173 WriteAttributes (reader, defattr);
174 reader.MoveToElement ();
175 if (reader.IsEmptyElement)
178 int depth = reader.Depth;
181 WriteNode (reader, defattr);
182 } while (depth < reader.Depth);
183 WriteFullEndElement ();
186 // In case of XmlAttribute, don't proceed reader.
187 case XmlNodeType.Attribute:
189 case XmlNodeType.Text:
190 WriteString (reader.Value);
192 case XmlNodeType.CDATA:
193 WriteCData (reader.Value);
195 case XmlNodeType.EntityReference:
196 WriteEntityRef (reader.Name);
198 case XmlNodeType.ProcessingInstruction:
199 WriteProcessingInstruction (reader.Name, reader.Value);
201 case XmlNodeType.Comment:
202 WriteComment (reader.Value);
204 case XmlNodeType.DocumentType:
205 WriteDocType (reader.Name,
206 reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
208 case XmlNodeType.SignificantWhitespace:
209 goto case XmlNodeType.Whitespace;
210 case XmlNodeType.Whitespace:
211 WriteWhitespace (reader.Value);
213 case XmlNodeType.EndElement:
214 WriteFullEndElement ();
216 case XmlNodeType.EndEntity:
218 case XmlNodeType.XmlDeclaration:
219 // FIXME: It seems different from MS way, but I have
220 // no other idea to write start document statefully.
221 string st = reader.GetAttribute ("standalone");
222 if (st != null && st != String.Empty)
223 WriteStartDocument (st.ToLower () == "yes");
225 WriteStartDocument ();
228 throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
233 public abstract void WriteProcessingInstruction (string name, string text);
235 public abstract void WriteQualifiedName (string localName, string ns);
237 public abstract void WriteRaw (string data);
239 public abstract void WriteRaw (char[] buffer, int index, int count);
241 public void WriteStartAttribute (string localName, string ns)
243 WriteStartAttribute (null, localName, ns);
246 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
248 public abstract void WriteStartDocument ();
250 public abstract void WriteStartDocument (bool standalone);
252 public void WriteStartElement (string localName)
254 WriteStartElement (null, localName, null);
257 public void WriteStartElement (string localName, string ns)
259 WriteStartElement (null, localName, ns);
262 public abstract void WriteStartElement (string prefix, string localName, string ns);
264 public abstract void WriteString (string text);
266 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
268 public abstract void WriteWhitespace (string ws);