XmlTextWriter.cs 20 KB

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