XmlTextWriter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. //
  2. // System.Xml.XmlTextWriter
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. // [FIXME]
  10. // Document state should be considered.
  11. //
  12. using System;
  13. using System.Collections;
  14. using System.IO;
  15. using System.Text;
  16. namespace System.Xml
  17. {
  18. public class XmlTextWriter : XmlWriter
  19. {
  20. #region Fields
  21. TextWriter w;
  22. bool nullEncoding = false;
  23. bool openWriter = true;
  24. bool openStartElement = false;
  25. bool openStartAttribute = false;
  26. bool documentStarted = false;
  27. bool namespaces = true;
  28. bool openAttribute = false;
  29. bool attributeWrittenForElement = false;
  30. Stack openElements = new Stack ();
  31. Formatting formatting = Formatting.None;
  32. int indentation = 2;
  33. char indentChar = ' ';
  34. string indentChars = " ";
  35. char quoteChar = '\"';
  36. int indentLevel = 0;
  37. string indentFormatting;
  38. Stream baseStream = null;
  39. string xmlLang = null;
  40. XmlSpace xmlSpace = XmlSpace.None;
  41. bool openXmlLang = false;
  42. bool openXmlSpace = false;
  43. string openElementPrefix;
  44. string openElementNS;
  45. bool openElementNsAdded;
  46. bool hasRoot = false;
  47. Hashtable writtenAttributes = new Hashtable ();
  48. ArrayList newAttributeNamespaces = new ArrayList ();
  49. bool checkMultipleAttributes = false;
  50. #endregion
  51. #region Constructors
  52. public XmlTextWriter (TextWriter w) : base ()
  53. {
  54. this.w = w;
  55. nullEncoding = (w.Encoding == null);
  56. try {
  57. baseStream = ((StreamWriter)w).BaseStream;
  58. }
  59. catch (Exception) { }
  60. }
  61. public XmlTextWriter (Stream w, Encoding encoding) : base ()
  62. {
  63. if (encoding == null) {
  64. nullEncoding = true;
  65. encoding = new UTF8Encoding ();
  66. }
  67. this.w = new StreamWriter(w, encoding);
  68. baseStream = w;
  69. }
  70. public XmlTextWriter (string filename, Encoding encoding) :
  71. this (new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None), encoding)
  72. {
  73. }
  74. #endregion
  75. #region Properties
  76. public Stream BaseStream {
  77. get { return baseStream; }
  78. }
  79. public Formatting Formatting {
  80. get { return formatting; }
  81. set { formatting = value; }
  82. }
  83. private bool IndentingOverriden
  84. {
  85. get {
  86. if (openElements.Count == 0)
  87. return false;
  88. else
  89. return (((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden);
  90. }
  91. set {
  92. if (openElements.Count > 0)
  93. ((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden = value;
  94. }
  95. }
  96. public int Indentation {
  97. get { return indentation; }
  98. set {
  99. indentation = value;
  100. UpdateIndentChars ();
  101. }
  102. }
  103. public char IndentChar {
  104. get { return indentChar; }
  105. set {
  106. indentChar = value;
  107. UpdateIndentChars ();
  108. }
  109. }
  110. public bool Namespaces {
  111. get { return namespaces; }
  112. set {
  113. if (ws != WriteState.Start)
  114. throw new InvalidOperationException ("NotInWriteState.");
  115. namespaces = value;
  116. }
  117. }
  118. public char QuoteChar {
  119. get { return quoteChar; }
  120. set {
  121. if ((value != '\'') && (value != '\"'))
  122. throw new ArgumentException ("This is an invalid XML attribute quote character. Valid attribute quote characters are ' and \".");
  123. quoteChar = value;
  124. }
  125. }
  126. public override WriteState WriteState {
  127. get { return ws; }
  128. }
  129. public override string XmlLang {
  130. get {
  131. string xmlLang = null;
  132. int i;
  133. for (i = 0; i < openElements.Count; i++)
  134. {
  135. xmlLang = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlLang;
  136. if (xmlLang != null)
  137. break;
  138. }
  139. return xmlLang;
  140. }
  141. }
  142. public override XmlSpace XmlSpace {
  143. get {
  144. XmlSpace xmlSpace = XmlSpace.None;
  145. int i;
  146. for (i = 0; i < openElements.Count; i++)
  147. {
  148. xmlSpace = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlSpace;
  149. if (xmlSpace != XmlSpace.None)
  150. break;
  151. }
  152. return xmlSpace;
  153. }
  154. }
  155. #endregion
  156. #region Methods
  157. private void AddMissingElementXmlns ()
  158. {
  159. // output namespace declaration if not exist.
  160. string prefix = openElementPrefix;
  161. string ns = openElementNS;
  162. openElementPrefix = null;
  163. openElementNS = null;
  164. // LAMESPEC: If prefix was already assigned another nsuri, then this element's nsuri goes away!
  165. if (ns != null)
  166. {
  167. string formatXmlns = String.Empty;
  168. if (ns != String.Empty)
  169. {
  170. if (prefix == String.Empty)
  171. prefix = namespaceManager.LookupPrefix (ns);
  172. if (openElementNsAdded)
  173. {
  174. if (prefix != string.Empty)
  175. formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
  176. else
  177. formatXmlns = String.Format (" xmlns={0}{1}{0}", quoteChar, ns);
  178. }
  179. }
  180. else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace (prefix) != ns))
  181. {
  182. namespaceManager.AddNamespace (prefix, ns);
  183. formatXmlns = String.Format (" xmlns={0}{0}", quoteChar);
  184. }
  185. if(formatXmlns != String.Empty) {
  186. string xmlns = formatXmlns.Trim ();
  187. if (checkMultipleAttributes && !writtenAttributes.Contains (xmlns.Substring (0, xmlns.IndexOf ('='))))
  188. w.Write(formatXmlns);
  189. }
  190. }
  191. for (int n=0; n<newAttributeNamespaces.Count; n++)
  192. {
  193. string ans = (string)newAttributeNamespaces[n];
  194. string aprefix = namespaceManager.LookupPrefix (ans);
  195. string formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", aprefix, quoteChar, ans);
  196. w.Write(formatXmlns);
  197. }
  198. newAttributeNamespaces.Clear ();
  199. }
  200. private void CheckState ()
  201. {
  202. if (!openWriter) {
  203. throw new InvalidOperationException ("The Writer is closed.");
  204. }
  205. if ((documentStarted == true) && (formatting == Formatting.Indented) && (!IndentingOverriden)) {
  206. indentFormatting = w.NewLine;
  207. if (indentLevel > 0) {
  208. for (int i = 0; i < indentLevel; i++)
  209. indentFormatting += indentChars;
  210. }
  211. }
  212. else
  213. indentFormatting = "";
  214. documentStarted = true;
  215. }
  216. public override void Close ()
  217. {
  218. CloseOpenAttributeAndElements ();
  219. w.Close();
  220. ws = WriteState.Closed;
  221. openWriter = false;
  222. }
  223. private void CloseOpenAttributeAndElements ()
  224. {
  225. if (openAttribute)
  226. WriteEndAttribute ();
  227. while (openElements.Count > 0) {
  228. WriteEndElement();
  229. }
  230. }
  231. private void CloseStartElement ()
  232. {
  233. if (!openStartElement)
  234. return;
  235. AddMissingElementXmlns ();
  236. w.Write (">");
  237. ws = WriteState.Content;
  238. openStartElement = false;
  239. attributeWrittenForElement = false;
  240. checkMultipleAttributes = false;
  241. writtenAttributes.Clear ();
  242. newAttributeNamespaces.Clear ();
  243. }
  244. public override void Flush ()
  245. {
  246. // CloseOpenAttributeAndElements ();
  247. w.Flush ();
  248. }
  249. public override string LookupPrefix (string ns)
  250. {
  251. string prefix = namespaceManager.LookupPrefix (ns);
  252. // XmlNamespaceManager has changed to return null when NSURI not found.
  253. // (Contradiction to the documentation.)
  254. //if (prefix == String.Empty)
  255. // prefix = null;
  256. return prefix;
  257. }
  258. private void UpdateIndentChars ()
  259. {
  260. indentChars = "";
  261. for (int i = 0; i < indentation; i++)
  262. indentChars += indentChar;
  263. }
  264. public override void WriteBase64 (byte[] buffer, int index, int count)
  265. {
  266. CheckState ();
  267. if (!openAttribute) {
  268. IndentingOverriden = true;
  269. CloseStartElement ();
  270. }
  271. w.Write (Convert.ToBase64String (buffer, index, count));
  272. }
  273. [MonoTODO]
  274. public override void WriteBinHex (byte[] buffer, int index, int count)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. public override void WriteCData (string text)
  279. {
  280. if (text.IndexOf("]]>") > 0)
  281. throw new ArgumentException ();
  282. CheckState ();
  283. CloseStartElement ();
  284. w.Write("<![CDATA[{0}]]>", text);
  285. }
  286. public override void WriteCharEntity (char ch)
  287. {
  288. Int16 intCh = (Int16)ch;
  289. // Make sure the character is not in the surrogate pair
  290. // character range, 0xd800- 0xdfff
  291. if ((intCh >= -10240) && (intCh <= -8193))
  292. throw new ArgumentException ("Surrogate Pair is invalid.");
  293. w.Write("&#x{0:X};", intCh);
  294. }
  295. [MonoTODO]
  296. public override void WriteChars (char[] buffer, int index, int count)
  297. {
  298. throw new NotImplementedException ();
  299. }
  300. public override void WriteComment (string text)
  301. {
  302. if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
  303. throw new ArgumentException ();
  304. }
  305. CheckState ();
  306. CloseStartElement ();
  307. w.Write ("<!--{0}-->", text);
  308. }
  309. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  310. {
  311. if (name == null || name.Trim ().Length == 0)
  312. throw new ArgumentException ("Invalid DOCTYPE name", "name");
  313. w.Write ("<!DOCTYPE ");
  314. w.Write (name);
  315. if (pubid != null) {
  316. w.Write (String.Format (" PUBLIC {0}{1}{0} {0}{2}{0}", quoteChar, pubid, sysid));
  317. } else if (sysid != null) {
  318. w.Write (String.Format (" SYSTEM {0}{1}{0}", quoteChar, sysid));
  319. }
  320. if (subset != null)
  321. w.Write ("[" + subset + "]");
  322. w.Write('>');
  323. }
  324. public override void WriteEndAttribute ()
  325. {
  326. if (!openAttribute)
  327. throw new InvalidOperationException("Token EndAttribute in state Start would result in an invalid XML document.");
  328. CheckState ();
  329. if (openXmlLang) {
  330. w.Write (xmlLang);
  331. openXmlLang = false;
  332. ((XmlTextWriterOpenElement)openElements.Peek()).XmlLang = xmlLang;
  333. }
  334. if (openXmlSpace)
  335. {
  336. w.Write (xmlSpace.ToString ().ToLower ());
  337. openXmlSpace = false;
  338. ((XmlTextWriterOpenElement)openElements.Peek()).XmlSpace = xmlSpace;
  339. }
  340. w.Write ("{0}", quoteChar);
  341. openAttribute = false;
  342. }
  343. public override void WriteEndDocument ()
  344. {
  345. CloseOpenAttributeAndElements ();
  346. if (!hasRoot)
  347. throw new ArgumentException ("This document does not have a root element.");
  348. ws = WriteState.Start;
  349. hasRoot = false;
  350. }
  351. public override void WriteEndElement ()
  352. {
  353. WriteEndElementInternal (false);
  354. }
  355. private void WriteEndElementInternal (bool fullEndElement)
  356. {
  357. if (openElements.Count == 0)
  358. throw new InvalidOperationException("There was no XML start tag open.");
  359. indentLevel--;
  360. CheckState ();
  361. AddMissingElementXmlns ();
  362. if (openStartElement) {
  363. if (openAttribute)
  364. WriteEndAttribute ();
  365. if (fullEndElement)
  366. w.Write ("></{0}>", ((XmlTextWriterOpenElement)openElements.Peek ()).Name);
  367. else
  368. w.Write (" />");
  369. openElements.Pop ();
  370. openStartElement = false;
  371. } else {
  372. w.Write ("{0}</{1}>", indentFormatting, openElements.Pop ());
  373. }
  374. namespaceManager.PopScope();
  375. }
  376. public override void WriteEntityRef (string name)
  377. {
  378. WriteRaw ("&");
  379. WriteStringInternal (name, true);
  380. WriteRaw (";");
  381. }
  382. public override void WriteFullEndElement ()
  383. {
  384. WriteEndElementInternal (true);
  385. }
  386. private void CheckValidChars (string name, bool firstOnlyLetter)
  387. {
  388. foreach (char c in name) {
  389. if (XmlConvert.IsInvalid (c, firstOnlyLetter))
  390. throw new ArgumentException ("There is an invalid character: '" + c +
  391. "'", "name");
  392. }
  393. }
  394. public override void WriteName (string name)
  395. {
  396. CheckValidChars (name, true);
  397. w.Write (name);
  398. }
  399. public override void WriteNmToken (string name)
  400. {
  401. CheckValidChars (name, false);
  402. w.Write (name);
  403. }
  404. public override void WriteProcessingInstruction (string name, string text)
  405. {
  406. if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
  407. throw new ArgumentException ();
  408. }
  409. CheckState ();
  410. CloseStartElement ();
  411. w.Write ("{0}<?{1} {2}?>", indentFormatting, name, text);
  412. }
  413. [MonoTODO]
  414. public override void WriteQualifiedName (string localName, string ns)
  415. {
  416. if (localName == null || localName == String.Empty)
  417. throw new ArgumentException ();
  418. CheckState ();
  419. if (!openAttribute)
  420. CloseStartElement ();
  421. string prefix = namespaceManager.LookupPrefix (ns);
  422. w.Write ("{0}:{1}", prefix, localName);
  423. }
  424. public override void WriteRaw (string data)
  425. {
  426. WriteStringInternal (data, false);
  427. }
  428. public override void WriteRaw (char[] buffer, int index, int count)
  429. {
  430. // WriteRawInternal (new string (buffer, index, count));
  431. WriteStringInternal (new string (buffer, index, count), false);
  432. }
  433. public override void WriteStartAttribute (string prefix, string localName, string ns)
  434. {
  435. if ((prefix == "xml") && (localName == "lang"))
  436. openXmlLang = true;
  437. if ((prefix == "xml") && (localName == "space"))
  438. openXmlSpace = true;
  439. if ((prefix == "xmlns") && (localName.ToLower ().StartsWith ("xml")))
  440. throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML: " + prefix + ":" + localName);
  441. if ((prefix == "xmlns") && (ns != "http://www.w3.org/2000/xmlns/"))
  442. throw new ArgumentException ("The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'");
  443. CheckState ();
  444. if (ws == WriteState.Content)
  445. throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
  446. if (prefix == null)
  447. prefix = String.Empty;
  448. if (ns == null)
  449. ns = String.Empty;
  450. string formatPrefix = "";
  451. string formatSpace = "";
  452. if (ns != String.Empty && prefix != "xmlns")
  453. {
  454. string existingPrefix = namespaceManager.LookupPrefix (ns);
  455. if (existingPrefix == null)
  456. {
  457. newAttributeNamespaces.Add (ns);
  458. if (prefix == "" || namespaceManager.LookupNamespace (prefix) != null)
  459. prefix = "d" + indentLevel + "p" + newAttributeNamespaces.Count;
  460. namespaceManager.AddNamespace (prefix, ns);
  461. }
  462. if (prefix == String.Empty && ns != "http://www.w3.org/2000/xmlns/")
  463. prefix = (existingPrefix == null) ?
  464. String.Empty : existingPrefix;
  465. }
  466. if (prefix != String.Empty)
  467. {
  468. formatPrefix = prefix + ":";
  469. }
  470. if (openStartElement || attributeWrittenForElement)
  471. formatSpace = " ";
  472. // If already written, then break up.
  473. if (checkMultipleAttributes &&
  474. writtenAttributes.Contains (formatPrefix + localName))
  475. return;
  476. w.Write ("{0}{1}{2}={3}", formatSpace, formatPrefix, localName, quoteChar);
  477. if (checkMultipleAttributes)
  478. writtenAttributes.Add (formatPrefix + localName, formatPrefix + localName);
  479. openAttribute = true;
  480. attributeWrittenForElement = true;
  481. ws = WriteState.Attribute;
  482. /* This is managed in WriteAttributeString.
  483. if (prefix == String.Empty && localName == "xmlns") {
  484. if (namespaceManager.LookupNamespace (prefix) == null)
  485. namespaceManager.AddNamespace (prefix, ns);
  486. } else if (prefix == "xmlns") {
  487. if (namespaceManager.LookupNamespace (localName) == null)
  488. namespaceManager.AddNamespace (localName, ns);
  489. }
  490. */
  491. }
  492. public override void WriteStartDocument ()
  493. {
  494. WriteStartDocument ("");
  495. }
  496. public override void WriteStartDocument (bool standalone)
  497. {
  498. string standaloneFormatting;
  499. if (standalone == true)
  500. standaloneFormatting = String.Format (" standalone={0}yes{0}", quoteChar);
  501. else
  502. standaloneFormatting = String.Format (" standalone={0}no{0}", quoteChar);
  503. WriteStartDocument (standaloneFormatting);
  504. }
  505. private void WriteStartDocument (string standaloneFormatting)
  506. {
  507. if (documentStarted == true)
  508. throw new InvalidOperationException("WriteStartDocument should be the first call.");
  509. if (hasRoot)
  510. throw new XmlException ("WriteStartDocument called twice.");
  511. CheckState ();
  512. string encodingFormatting = "";
  513. if (!nullEncoding)
  514. encodingFormatting = String.Format (" encoding={0}{1}{0}", quoteChar, w.Encoding.WebName);
  515. w.Write("<?xml version={0}1.0{0}{1}{2}?>", quoteChar, encodingFormatting, standaloneFormatting);
  516. ws = WriteState.Prolog;
  517. }
  518. public override void WriteStartElement (string prefix, string localName, string ns)
  519. {
  520. if (!Namespaces && (((prefix != null) && (prefix != String.Empty))
  521. || ((ns != null) && (ns != String.Empty))))
  522. throw new ArgumentException ("Cannot set the namespace if Namespaces is 'false'.");
  523. WriteStartElementInternal (prefix, localName, ns);
  524. }
  525. private void WriteStartElementInternal (string prefix, string localName, string ns)
  526. {
  527. if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
  528. throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
  529. hasRoot = true;
  530. CheckState ();
  531. CloseStartElement ();
  532. writtenAttributes.Clear ();
  533. newAttributeNamespaces.Clear ();
  534. checkMultipleAttributes = true;
  535. if (prefix == null)
  536. prefix = namespaceManager.LookupPrefix (ns);
  537. if (prefix == null)
  538. prefix = String.Empty;
  539. string formatPrefix = "";
  540. if(ns != null) {
  541. if (prefix != String.Empty)
  542. formatPrefix = prefix + ":";
  543. }
  544. w.Write ("{0}<{1}{2}", indentFormatting, formatPrefix, localName);
  545. openElements.Push (new XmlTextWriterOpenElement (formatPrefix + localName));
  546. ws = WriteState.Element;
  547. openStartElement = true;
  548. openElementNS = ns;
  549. openElementPrefix = prefix;
  550. openElementNsAdded = false;
  551. namespaceManager.PushScope ();
  552. indentLevel++;
  553. if (ns != null && ns != string.Empty && namespaceManager.LookupPrefix (ns) == null)
  554. {
  555. namespaceManager.AddNamespace (prefix, ns);
  556. openElementNsAdded = true;
  557. }
  558. }
  559. public override void WriteString (string text)
  560. {
  561. if (ws == WriteState.Prolog)
  562. throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
  563. WriteStringInternal (text, true);
  564. }
  565. private string NormalizeAttributeString (string value)
  566. {
  567. return value.Replace ("\r", "&#xD;").Replace ("\n", "&#xA;");
  568. }
  569. private void WriteStringInternal (string text, bool entitize)
  570. {
  571. if (text == null)
  572. text = String.Empty;
  573. if (text != String.Empty) {
  574. CheckState ();
  575. if (entitize)
  576. {
  577. text = text.Replace ("&", "&amp;");
  578. text = text.Replace ("<", "&lt;");
  579. text = text.Replace (">", "&gt;");
  580. if (openAttribute)
  581. {
  582. if (quoteChar == '"')
  583. text = text.Replace ("\"", "&quot;");
  584. else
  585. text = text.Replace ("'", "&apos;");
  586. text = NormalizeAttributeString (text);
  587. }
  588. }
  589. if (!openAttribute)
  590. {
  591. IndentingOverriden = true;
  592. CloseStartElement ();
  593. }
  594. if (!openXmlLang && !openXmlSpace)
  595. w.Write (text);
  596. else
  597. {
  598. if (openXmlLang)
  599. xmlLang = text;
  600. else
  601. {
  602. switch (text)
  603. {
  604. case "default":
  605. xmlSpace = XmlSpace.Default;
  606. break;
  607. case "preserve":
  608. xmlSpace = XmlSpace.Preserve;
  609. break;
  610. default:
  611. throw new ArgumentException ("'{0}' is an invalid xml:space value.");
  612. }
  613. }
  614. }
  615. }
  616. }
  617. [MonoTODO]
  618. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  619. {
  620. throw new NotImplementedException ();
  621. }
  622. public override void WriteWhitespace (string ws)
  623. {
  624. foreach (char c in ws) {
  625. if ((c != ' ') && (c != '\t') && (c != '\r') && (c != '\n'))
  626. throw new ArgumentException ();
  627. }
  628. CheckState ();
  629. if (!openAttribute) {
  630. IndentingOverriden = true;
  631. CloseStartElement ();
  632. }
  633. w.Write (ws);
  634. }
  635. #endregion
  636. }
  637. }