XmlTextWriter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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[");
  293. w.Write (text);
  294. w.Write ("]]>");
  295. }
  296. public override void WriteCharEntity (char ch)
  297. {
  298. Int16 intCh = (Int16)ch;
  299. // Make sure the character is not in the surrogate pair
  300. // character range, 0xd800- 0xdfff
  301. if ((intCh >= -10240) && (intCh <= -8193))
  302. throw new ArgumentException ("Surrogate Pair is invalid.");
  303. w.Write("&#x{0:X};", intCh);
  304. }
  305. [MonoTODO]
  306. public override void WriteChars (char[] buffer, int index, int count)
  307. {
  308. throw new NotImplementedException ();
  309. }
  310. public override void WriteComment (string text)
  311. {
  312. if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
  313. throw new ArgumentException ();
  314. }
  315. CheckState ();
  316. CloseStartElement ();
  317. w.Write ("<!--");
  318. w.Write (text);
  319. w.Write ("-->");
  320. }
  321. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  322. {
  323. if (name == null || name.Trim ().Length == 0)
  324. throw new ArgumentException ("Invalid DOCTYPE name", "name");
  325. w.Write ("<!DOCTYPE ");
  326. w.Write (name);
  327. if (pubid != null) {
  328. w.Write (" PUBLIC ");
  329. w.Write (quoteChar);
  330. w.Write (pubid);
  331. w.Write (quoteChar);
  332. w.Write (quoteChar);
  333. w.Write (sysid);
  334. w.Write (quoteChar);
  335. } else if (sysid != null) {
  336. w.Write (" SYSTEM ");
  337. w.Write (quoteChar);
  338. w.Write (sysid);
  339. w.Write (quoteChar);
  340. }
  341. if (subset != null) {
  342. w.Write ('[');
  343. w.Write (subset);
  344. w.Write (']');
  345. }
  346. w.Write('>');
  347. }
  348. public override void WriteEndAttribute ()
  349. {
  350. if (!openAttribute)
  351. throw new InvalidOperationException("Token EndAttribute in state Start would result in an invalid XML document.");
  352. CheckState ();
  353. if (openXmlLang) {
  354. w.Write (xmlLang);
  355. openXmlLang = false;
  356. ((XmlTextWriterOpenElement)openElements.Peek()).XmlLang = xmlLang;
  357. }
  358. if (openXmlSpace)
  359. {
  360. w.Write (xmlSpace.ToString ().ToLower ());
  361. openXmlSpace = false;
  362. ((XmlTextWriterOpenElement)openElements.Peek()).XmlSpace = xmlSpace;
  363. }
  364. w.Write (quoteChar);
  365. openAttribute = false;
  366. if (saveAttributeValue) {
  367. // add namespace
  368. namespaceManager.AddNamespace (
  369. savedAttributePrefix, savingAttributeValue);
  370. saveAttributeValue = false;
  371. savedAttributePrefix = String.Empty;
  372. savingAttributeValue = String.Empty;
  373. }
  374. }
  375. public override void WriteEndDocument ()
  376. {
  377. CloseOpenAttributeAndElements ();
  378. if (!hasRoot)
  379. throw new ArgumentException ("This document does not have a root element.");
  380. ws = WriteState.Start;
  381. hasRoot = false;
  382. }
  383. public override void WriteEndElement ()
  384. {
  385. WriteEndElementInternal (false);
  386. }
  387. private void WriteEndElementInternal (bool fullEndElement)
  388. {
  389. if (openElements.Count == 0)
  390. throw new InvalidOperationException("There was no XML start tag open.");
  391. indentLevel--;
  392. CheckState ();
  393. AddMissingElementXmlns ();
  394. if (openStartElement) {
  395. if (openAttribute)
  396. WriteEndAttribute ();
  397. if (fullEndElement) {
  398. w.Write ("></");
  399. w.Write (((XmlTextWriterOpenElement)openElements.Peek ()).Name);
  400. w.Write ('>');
  401. } else
  402. w.Write (" />");
  403. openElements.Pop ();
  404. openStartElement = false;
  405. } else {
  406. w.Write (indentFormatting);
  407. w.Write ("</");
  408. w.Write (openElements.Pop ());
  409. w.Write ('>');
  410. }
  411. namespaceManager.PopScope();
  412. }
  413. public override void WriteEntityRef (string name)
  414. {
  415. WriteRaw ("&");
  416. WriteStringInternal (name, true);
  417. WriteRaw (";");
  418. }
  419. public override void WriteFullEndElement ()
  420. {
  421. WriteEndElementInternal (true);
  422. }
  423. private void CheckValidName (string name, bool firstOnlyLetter)
  424. {
  425. if (firstOnlyLetter && !XmlConstructs.IsNameStart (name [0]))
  426. throw new ArgumentException ("There is an invalid character: '" + name [0] +
  427. "'", "name");
  428. foreach (char c in name) {
  429. if (!XmlConstructs.IsName (c))
  430. throw new ArgumentException ("There is an invalid character: '" + c +
  431. "'", "name");
  432. }
  433. }
  434. public override void WriteName (string name)
  435. {
  436. CheckValidName (name, true);
  437. w.Write (name);
  438. }
  439. public override void WriteNmToken (string name)
  440. {
  441. CheckValidName (name, false);
  442. w.Write (name);
  443. }
  444. public override void WriteProcessingInstruction (string name, string text)
  445. {
  446. if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
  447. throw new ArgumentException ();
  448. }
  449. CheckState ();
  450. CloseStartElement ();
  451. w.Write (indentFormatting);
  452. w.Write ("<?");
  453. w.Write (name);
  454. w.Write (' ');
  455. w.Write (text);
  456. w.Write ("?>");
  457. }
  458. [MonoTODO]
  459. public override void WriteQualifiedName (string localName, string ns)
  460. {
  461. if (localName == null || localName == String.Empty)
  462. throw new ArgumentException ();
  463. CheckState ();
  464. if (!openAttribute)
  465. CloseStartElement ();
  466. w.Write (namespaceManager.LookupPrefix (ns));
  467. w.Write (':');
  468. w.Write (localName);
  469. }
  470. public override void WriteRaw (string data)
  471. {
  472. WriteStringInternal (data, false);
  473. }
  474. public override void WriteRaw (char[] buffer, int index, int count)
  475. {
  476. // WriteRawInternal (new string (buffer, index, count));
  477. WriteStringInternal (new string (buffer, index, count), false);
  478. }
  479. public override void WriteStartAttribute (string prefix, string localName, string ns)
  480. {
  481. if ((prefix == "xml") && (localName == "lang"))
  482. openXmlLang = true;
  483. if ((prefix == "xml") && (localName == "space"))
  484. openXmlSpace = true;
  485. if ((prefix == "xmlns") && (localName.ToLower ().StartsWith ("xml")))
  486. 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);
  487. if ((prefix == "xmlns") && (ns != XmlnsNamespace))
  488. throw new ArgumentException (String.Format ("The 'xmlns' attribute is bound to the reserved namespace '{0}'", XmlnsNamespace));
  489. CheckState ();
  490. if (ws == WriteState.Content)
  491. throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
  492. if (prefix == null)
  493. prefix = String.Empty;
  494. if (ns == null)
  495. ns = String.Empty;
  496. string formatPrefix = "";
  497. string formatSpace = "";
  498. if (ns != String.Empty && prefix != "xmlns")
  499. {
  500. string existingPrefix = namespaceManager.LookupPrefix (ns);
  501. if (existingPrefix == null)
  502. {
  503. newAttributeNamespaces.Add (ns);
  504. if (prefix == "" || namespaceManager.LookupNamespace (prefix) != null)
  505. prefix = "d" + indentLevel + "p" + newAttributeNamespaces.Count;
  506. namespaceManager.AddNamespace (prefix, ns);
  507. }
  508. if (prefix == String.Empty && ns != XmlnsNamespace)
  509. prefix = (existingPrefix == null) ?
  510. String.Empty : existingPrefix;
  511. }
  512. if (prefix != String.Empty)
  513. {
  514. formatPrefix = prefix + ":";
  515. }
  516. if (openStartElement || attributeWrittenForElement)
  517. formatSpace = " ";
  518. // If already written, then break up.
  519. // if (checkMultipleAttributes &&
  520. // writtenAttributes.Contains (formatPrefix + localName))
  521. // return;
  522. w.Write (formatSpace);
  523. w.Write (formatPrefix);
  524. w.Write (localName);
  525. w.Write ('=');
  526. w.Write (quoteChar);
  527. if (checkMultipleAttributes)
  528. writtenAttributes.Add (formatPrefix + localName, formatPrefix + localName);
  529. openAttribute = true;
  530. attributeWrittenForElement = true;
  531. ws = WriteState.Attribute;
  532. if (prefix == "xmlns" || prefix == String.Empty && localName == "xmlns") {
  533. saveAttributeValue = true;
  534. savedAttributePrefix = (prefix == "xmlns") ? localName : String.Empty;
  535. savingAttributeValue = String.Empty;
  536. }
  537. }
  538. public override void WriteStartDocument ()
  539. {
  540. WriteStartDocument ("");
  541. }
  542. public override void WriteStartDocument (bool standalone)
  543. {
  544. string standaloneFormatting;
  545. if (standalone == true)
  546. standaloneFormatting = String.Format (" standalone={0}yes{0}", quoteChar);
  547. else
  548. standaloneFormatting = String.Format (" standalone={0}no{0}", quoteChar);
  549. WriteStartDocument (standaloneFormatting);
  550. }
  551. private void WriteStartDocument (string standaloneFormatting)
  552. {
  553. if (documentStarted == true)
  554. throw new InvalidOperationException("WriteStartDocument should be the first call.");
  555. if (hasRoot)
  556. throw new XmlException ("WriteStartDocument called twice.");
  557. CheckState ();
  558. string encodingFormatting = "";
  559. if (!nullEncoding)
  560. encodingFormatting = String.Format (" encoding={0}{1}{0}", quoteChar, w.Encoding.WebName);
  561. w.Write("<?xml version={0}1.0{0}{1}{2}?>", quoteChar, encodingFormatting, standaloneFormatting);
  562. ws = WriteState.Prolog;
  563. }
  564. public override void WriteStartElement (string prefix, string localName, string ns)
  565. {
  566. if (!Namespaces && (((prefix != null) && (prefix != String.Empty))
  567. || ((ns != null) && (ns != String.Empty))))
  568. throw new ArgumentException ("Cannot set the namespace if Namespaces is 'false'.");
  569. WriteStartElementInternal (prefix, localName, ns);
  570. }
  571. private void WriteStartElementInternal (string prefix, string localName, string ns)
  572. {
  573. if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
  574. throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
  575. hasRoot = true;
  576. CheckState ();
  577. CloseStartElement ();
  578. writtenAttributes.Clear ();
  579. newAttributeNamespaces.Clear ();
  580. checkMultipleAttributes = true;
  581. if (prefix == null)
  582. prefix = namespaceManager.LookupPrefix (ns);
  583. if (prefix == null)
  584. prefix = String.Empty;
  585. string formatName = localName;
  586. if (ns != null && prefix != String.Empty)
  587. formatName = prefix + ":" + localName;
  588. w.Write (indentFormatting);
  589. w.Write ('<');
  590. w.Write (formatName);
  591. openElements.Push (new XmlTextWriterOpenElement (formatName));
  592. ws = WriteState.Element;
  593. openStartElement = true;
  594. openElementNS = ns;
  595. openElementPrefix = prefix;
  596. openElementNsAdded = false;
  597. namespaceManager.PushScope ();
  598. indentLevel++;
  599. if (ns != null && ns != string.Empty && namespaceManager.LookupPrefix (ns) == null)
  600. {
  601. namespaceManager.AddNamespace (prefix, ns);
  602. openElementNsAdded = true;
  603. }
  604. }
  605. public override void WriteString (string text)
  606. {
  607. if (ws == WriteState.Prolog)
  608. throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
  609. WriteStringInternal (text, true);
  610. // MS.NET (1.0) saves attribute value only at WriteString.
  611. if (saveAttributeValue)
  612. // In most cases it will be called one time, so simply use string + string.
  613. savingAttributeValue += text;
  614. }
  615. private string NormalizeAttributeString (string value)
  616. {
  617. return value.Replace ("\r", "&#xD;").Replace ("\n", "&#xA;");
  618. }
  619. private void WriteStringInternal (string text, bool entitize)
  620. {
  621. if (text == null)
  622. text = String.Empty;
  623. if (text != String.Empty) {
  624. CheckState ();
  625. if (entitize)
  626. {
  627. text = text.Replace ("&", "&amp;");
  628. text = text.Replace ("<", "&lt;");
  629. text = text.Replace (">", "&gt;");
  630. if (openAttribute)
  631. {
  632. if (quoteChar == '"')
  633. text = text.Replace ("\"", "&quot;");
  634. else
  635. text = text.Replace ("'", "&apos;");
  636. text = NormalizeAttributeString (text);
  637. }
  638. }
  639. if (!openAttribute)
  640. {
  641. IndentingOverriden = true;
  642. CloseStartElement ();
  643. }
  644. if (!openXmlLang && !openXmlSpace)
  645. w.Write (text);
  646. else
  647. {
  648. if (openXmlLang)
  649. xmlLang = text;
  650. else
  651. {
  652. switch (text)
  653. {
  654. case "default":
  655. xmlSpace = XmlSpace.Default;
  656. break;
  657. case "preserve":
  658. xmlSpace = XmlSpace.Preserve;
  659. break;
  660. default:
  661. throw new ArgumentException ("'{0}' is an invalid xml:space value.");
  662. }
  663. }
  664. }
  665. }
  666. }
  667. [MonoTODO]
  668. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  669. {
  670. throw new NotImplementedException ();
  671. }
  672. public override void WriteWhitespace (string ws)
  673. {
  674. if (!XmlChar.IsWhitespace (ws))
  675. throw new ArgumentException ("Invalid Whitespace");
  676. CheckState ();
  677. if (!openAttribute) {
  678. IndentingOverriden = true;
  679. CloseStartElement ();
  680. }
  681. w.Write (ws);
  682. }
  683. #endregion
  684. }
  685. }