XmlDictionaryReader.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Xml
  5. {
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Collections;
  9. using System.IO;
  10. using System.Xml;
  11. using System.Diagnostics;
  12. using System.Text;
  13. using System.Runtime.Serialization;
  14. using System.Globalization;
  15. public delegate void OnXmlDictionaryReaderClose(XmlDictionaryReader reader);
  16. public abstract class XmlDictionaryReader : XmlReader
  17. {
  18. internal const int MaxInitialArrayLength = 65535;
  19. static public XmlDictionaryReader CreateDictionaryReader(XmlReader reader)
  20. {
  21. if (reader == null)
  22. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  23. XmlDictionaryReader dictionaryReader = reader as XmlDictionaryReader;
  24. if (dictionaryReader == null)
  25. {
  26. dictionaryReader = new XmlWrappedReader(reader, null);
  27. }
  28. return dictionaryReader;
  29. }
  30. public static XmlDictionaryReader CreateBinaryReader(byte[] buffer, XmlDictionaryReaderQuotas quotas)
  31. {
  32. if (buffer == null)
  33. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
  34. return CreateBinaryReader(buffer, 0, buffer.Length, quotas);
  35. }
  36. public static XmlDictionaryReader CreateBinaryReader(byte[] buffer, int offset, int count, XmlDictionaryReaderQuotas quotas)
  37. {
  38. return CreateBinaryReader(buffer, offset, count, null, quotas);
  39. }
  40. public static XmlDictionaryReader CreateBinaryReader(byte[] buffer, int offset, int count, IXmlDictionary dictionary, XmlDictionaryReaderQuotas quotas)
  41. {
  42. return CreateBinaryReader(buffer, offset, count, dictionary, quotas, null);
  43. }
  44. public static XmlDictionaryReader CreateBinaryReader(byte[] buffer, int offset, int count, IXmlDictionary dictionary, XmlDictionaryReaderQuotas quotas, XmlBinaryReaderSession session)
  45. {
  46. return CreateBinaryReader(buffer, offset, count, dictionary, quotas, session, null);
  47. }
  48. public static XmlDictionaryReader CreateBinaryReader(byte[] buffer, int offset, int count,
  49. IXmlDictionary dictionary,
  50. XmlDictionaryReaderQuotas quotas,
  51. XmlBinaryReaderSession session,
  52. OnXmlDictionaryReaderClose onClose)
  53. {
  54. XmlBinaryReader reader = new XmlBinaryReader();
  55. reader.SetInput(buffer, offset, count, dictionary, quotas, session, onClose);
  56. return reader;
  57. }
  58. public static XmlDictionaryReader CreateBinaryReader(Stream stream, XmlDictionaryReaderQuotas quotas)
  59. {
  60. return CreateBinaryReader(stream, null, quotas);
  61. }
  62. public static XmlDictionaryReader CreateBinaryReader(Stream stream, IXmlDictionary dictionary, XmlDictionaryReaderQuotas quotas)
  63. {
  64. return CreateBinaryReader(stream, dictionary, quotas, null);
  65. }
  66. public static XmlDictionaryReader CreateBinaryReader(Stream stream, IXmlDictionary dictionary, XmlDictionaryReaderQuotas quotas, XmlBinaryReaderSession session)
  67. {
  68. return CreateBinaryReader(stream, dictionary, quotas, session, null);
  69. }
  70. public static XmlDictionaryReader CreateBinaryReader(Stream stream,
  71. IXmlDictionary dictionary,
  72. XmlDictionaryReaderQuotas quotas,
  73. XmlBinaryReaderSession session,
  74. OnXmlDictionaryReaderClose onClose)
  75. {
  76. XmlBinaryReader reader = new XmlBinaryReader();
  77. reader.SetInput(stream, dictionary, quotas, session, onClose);
  78. return reader;
  79. }
  80. static public XmlDictionaryReader CreateTextReader(byte[] buffer, XmlDictionaryReaderQuotas quotas)
  81. {
  82. if (buffer == null)
  83. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("buffer");
  84. return CreateTextReader(buffer, 0, buffer.Length, quotas);
  85. }
  86. static public XmlDictionaryReader CreateTextReader(byte[] buffer, int offset, int count, XmlDictionaryReaderQuotas quotas)
  87. {
  88. return CreateTextReader(buffer, offset, count, null, quotas, null);
  89. }
  90. static public XmlDictionaryReader CreateTextReader(byte[] buffer, int offset, int count,
  91. Encoding encoding,
  92. XmlDictionaryReaderQuotas quotas,
  93. OnXmlDictionaryReaderClose onClose)
  94. {
  95. XmlUTF8TextReader reader = new XmlUTF8TextReader();
  96. reader.SetInput(buffer, offset, count, encoding, quotas, onClose);
  97. return reader;
  98. }
  99. static public XmlDictionaryReader CreateTextReader(Stream stream, XmlDictionaryReaderQuotas quotas)
  100. {
  101. return CreateTextReader(stream, null, quotas, null);
  102. }
  103. static public XmlDictionaryReader CreateTextReader(Stream stream, Encoding encoding,
  104. XmlDictionaryReaderQuotas quotas,
  105. OnXmlDictionaryReaderClose onClose)
  106. {
  107. XmlUTF8TextReader reader = new XmlUTF8TextReader();
  108. reader.SetInput(stream, encoding, quotas, onClose);
  109. return reader;
  110. }
  111. static public XmlDictionaryReader CreateMtomReader(Stream stream, Encoding encoding, XmlDictionaryReaderQuotas quotas)
  112. {
  113. if (encoding == null)
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding");
  115. return CreateMtomReader(stream, new Encoding[1] { encoding }, quotas);
  116. }
  117. static public XmlDictionaryReader CreateMtomReader(Stream stream, Encoding[] encodings, XmlDictionaryReaderQuotas quotas)
  118. {
  119. return CreateMtomReader(stream, encodings, null, quotas);
  120. }
  121. static public XmlDictionaryReader CreateMtomReader(Stream stream, Encoding[] encodings, string contentType, XmlDictionaryReaderQuotas quotas)
  122. {
  123. return CreateMtomReader(stream, encodings, contentType, quotas, int.MaxValue, null);
  124. }
  125. static public XmlDictionaryReader CreateMtomReader(Stream stream, Encoding[] encodings, string contentType,
  126. XmlDictionaryReaderQuotas quotas, int maxBufferSize, OnXmlDictionaryReaderClose onClose)
  127. {
  128. XmlMtomReader reader = new XmlMtomReader();
  129. reader.SetInput(stream, encodings, contentType, quotas, maxBufferSize, onClose);
  130. return reader;
  131. }
  132. static public XmlDictionaryReader CreateMtomReader(byte[] buffer, int offset, int count, Encoding encoding, XmlDictionaryReaderQuotas quotas)
  133. {
  134. if (encoding == null)
  135. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding");
  136. return CreateMtomReader(buffer, offset, count, new Encoding[1] { encoding }, quotas);
  137. }
  138. static public XmlDictionaryReader CreateMtomReader(byte[] buffer, int offset, int count, Encoding[] encodings, XmlDictionaryReaderQuotas quotas)
  139. {
  140. return CreateMtomReader(buffer, offset, count, encodings, null, quotas);
  141. }
  142. static public XmlDictionaryReader CreateMtomReader(byte[] buffer, int offset, int count, Encoding[] encodings, string contentType, XmlDictionaryReaderQuotas quotas)
  143. {
  144. return CreateMtomReader(buffer, offset, count, encodings, contentType, quotas, int.MaxValue, null);
  145. }
  146. static public XmlDictionaryReader CreateMtomReader(byte[] buffer, int offset, int count, Encoding[] encodings, string contentType,
  147. XmlDictionaryReaderQuotas quotas, int maxBufferSize, OnXmlDictionaryReaderClose onClose)
  148. {
  149. XmlMtomReader reader = new XmlMtomReader();
  150. reader.SetInput(buffer, offset, count, encodings, contentType, quotas, maxBufferSize, onClose);
  151. return reader;
  152. }
  153. #if NO
  154. static XmlDictionaryReader CreateMtomReader(byte[] buffer, int offset, int count, Encoding encoding)
  155. {
  156. return XmlMtomWriter.IsUTF8Encoding(encoding) ? new XmlUTF8TextReader(buffer, offset, count)
  157. : CreateReader(new MemoryStream(buffer, offset, count), encoding);
  158. }
  159. #endif
  160. public virtual bool CanCanonicalize
  161. {
  162. get
  163. {
  164. return false;
  165. }
  166. }
  167. public virtual XmlDictionaryReaderQuotas Quotas
  168. {
  169. get
  170. {
  171. return XmlDictionaryReaderQuotas.Max;
  172. }
  173. }
  174. public virtual void StartCanonicalization(Stream stream, bool includeComments, string[] inclusivePrefixes)
  175. {
  176. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  177. }
  178. public virtual void EndCanonicalization()
  179. {
  180. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  181. }
  182. public virtual void MoveToStartElement()
  183. {
  184. if (!IsStartElement())
  185. XmlExceptionHelper.ThrowStartElementExpected(this);
  186. }
  187. public virtual void MoveToStartElement(string name)
  188. {
  189. if (!IsStartElement(name))
  190. XmlExceptionHelper.ThrowStartElementExpected(this, name);
  191. }
  192. public virtual void MoveToStartElement(string localName, string namespaceUri)
  193. {
  194. if (!IsStartElement(localName, namespaceUri))
  195. XmlExceptionHelper.ThrowStartElementExpected(this, localName, namespaceUri);
  196. }
  197. public virtual void MoveToStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  198. {
  199. if (!IsStartElement(localName, namespaceUri))
  200. XmlExceptionHelper.ThrowStartElementExpected(this, localName, namespaceUri);
  201. }
  202. public virtual bool IsLocalName(string localName)
  203. {
  204. return this.LocalName == localName;
  205. }
  206. public virtual bool IsLocalName(XmlDictionaryString localName)
  207. {
  208. if (localName == null)
  209. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localName");
  210. return IsLocalName(localName.Value);
  211. }
  212. public virtual bool IsNamespaceUri(string namespaceUri)
  213. {
  214. if (namespaceUri == null)
  215. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  216. return this.NamespaceURI == namespaceUri;
  217. }
  218. public virtual bool IsNamespaceUri(XmlDictionaryString namespaceUri)
  219. {
  220. if (namespaceUri == null)
  221. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  222. return IsNamespaceUri(namespaceUri.Value);
  223. }
  224. public virtual void ReadFullStartElement()
  225. {
  226. MoveToStartElement();
  227. if (IsEmptyElement)
  228. XmlExceptionHelper.ThrowFullStartElementExpected(this);
  229. Read();
  230. }
  231. public virtual void ReadFullStartElement(string name)
  232. {
  233. MoveToStartElement(name);
  234. if (IsEmptyElement)
  235. XmlExceptionHelper.ThrowFullStartElementExpected(this, name);
  236. Read();
  237. }
  238. public virtual void ReadFullStartElement(string localName, string namespaceUri)
  239. {
  240. MoveToStartElement(localName, namespaceUri);
  241. if (IsEmptyElement)
  242. XmlExceptionHelper.ThrowFullStartElementExpected(this, localName, namespaceUri);
  243. Read();
  244. }
  245. public virtual void ReadFullStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  246. {
  247. MoveToStartElement(localName, namespaceUri);
  248. if (IsEmptyElement)
  249. XmlExceptionHelper.ThrowFullStartElementExpected(this, localName, namespaceUri);
  250. Read();
  251. }
  252. public virtual void ReadStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  253. {
  254. MoveToStartElement(localName, namespaceUri);
  255. Read();
  256. }
  257. public virtual bool IsStartElement(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  258. {
  259. return IsStartElement(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri));
  260. }
  261. public virtual int IndexOfLocalName(string[] localNames, string namespaceUri)
  262. {
  263. if (localNames == null)
  264. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localNames");
  265. if (namespaceUri == null)
  266. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  267. if (this.NamespaceURI == namespaceUri)
  268. {
  269. string localName = this.LocalName;
  270. for (int i = 0; i < localNames.Length; i++)
  271. {
  272. string value = localNames[i];
  273. if (value == null)
  274. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(string.Format(CultureInfo.InvariantCulture, "localNames[{0}]", i));
  275. if (localName == value)
  276. {
  277. return i;
  278. }
  279. }
  280. }
  281. return -1;
  282. }
  283. public virtual int IndexOfLocalName(XmlDictionaryString[] localNames, XmlDictionaryString namespaceUri)
  284. {
  285. if (localNames == null)
  286. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localNames");
  287. if (namespaceUri == null)
  288. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("namespaceUri");
  289. if (this.NamespaceURI == namespaceUri.Value)
  290. {
  291. string localName = this.LocalName;
  292. for (int i = 0; i < localNames.Length; i++)
  293. {
  294. XmlDictionaryString value = localNames[i];
  295. if (value == null)
  296. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(string.Format(CultureInfo.InvariantCulture, "localNames[{0}]", i));
  297. if (localName == value.Value)
  298. {
  299. return i;
  300. }
  301. }
  302. }
  303. return -1;
  304. }
  305. public virtual string GetAttribute(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  306. {
  307. return GetAttribute(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri));
  308. }
  309. public virtual bool TryGetBase64ContentLength(out int length)
  310. {
  311. length = 0;
  312. return false;
  313. }
  314. public virtual int ReadValueAsBase64(byte[] buffer, int offset, int count)
  315. {
  316. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException());
  317. }
  318. public virtual byte[] ReadContentAsBase64()
  319. {
  320. return ReadContentAsBase64(Quotas.MaxArrayLength, MaxInitialArrayLength);
  321. }
  322. internal byte[] ReadContentAsBase64(int maxByteArrayContentLength, int maxInitialCount)
  323. {
  324. int length;
  325. if (TryGetBase64ContentLength(out length))
  326. {
  327. if (length > maxByteArrayContentLength)
  328. XmlExceptionHelper.ThrowMaxArrayLengthExceeded(this, maxByteArrayContentLength);
  329. if (length <= maxInitialCount)
  330. {
  331. byte[] buffer = new byte[length];
  332. int read = 0;
  333. while (read < length)
  334. {
  335. int actual = ReadContentAsBase64(buffer, read, length - read);
  336. if (actual == 0)
  337. XmlExceptionHelper.ThrowBase64DataExpected(this);
  338. read += actual;
  339. }
  340. return buffer;
  341. }
  342. }
  343. return ReadContentAsBytes(true, maxByteArrayContentLength);
  344. }
  345. public override string ReadContentAsString()
  346. {
  347. return ReadContentAsString(Quotas.MaxStringContentLength);
  348. }
  349. protected string ReadContentAsString(int maxStringContentLength)
  350. {
  351. StringBuilder sb = null;
  352. string result = string.Empty;
  353. bool done = false;
  354. while (true)
  355. {
  356. switch (this.NodeType)
  357. {
  358. case XmlNodeType.Attribute:
  359. result = this.Value;
  360. break;
  361. case XmlNodeType.Text:
  362. case XmlNodeType.Whitespace:
  363. case XmlNodeType.SignificantWhitespace:
  364. case XmlNodeType.CDATA:
  365. // merge text content
  366. string value = this.Value;
  367. if (result.Length == 0)
  368. {
  369. result = value;
  370. }
  371. else
  372. {
  373. if (sb == null)
  374. sb = new StringBuilder(result);
  375. if (sb.Length > maxStringContentLength - value.Length)
  376. XmlExceptionHelper.ThrowMaxStringContentLengthExceeded(this, maxStringContentLength);
  377. sb.Append(value);
  378. }
  379. break;
  380. case XmlNodeType.ProcessingInstruction:
  381. case XmlNodeType.Comment:
  382. case XmlNodeType.EndEntity:
  383. // skip comments, pis and end entity nodes
  384. break;
  385. case XmlNodeType.EntityReference:
  386. if (this.CanResolveEntity)
  387. {
  388. this.ResolveEntity();
  389. break;
  390. }
  391. goto default;
  392. case XmlNodeType.Element:
  393. case XmlNodeType.EndElement:
  394. default:
  395. done = true;
  396. break;
  397. }
  398. if (done)
  399. break;
  400. if (this.AttributeCount != 0)
  401. ReadAttributeValue();
  402. else
  403. Read();
  404. }
  405. if (sb != null)
  406. result = sb.ToString();
  407. if (result.Length > maxStringContentLength)
  408. XmlExceptionHelper.ThrowMaxStringContentLengthExceeded(this, maxStringContentLength);
  409. return result;
  410. }
  411. public override string ReadString()
  412. {
  413. return ReadString(Quotas.MaxStringContentLength);
  414. }
  415. protected string ReadString(int maxStringContentLength)
  416. {
  417. if (this.ReadState != ReadState.Interactive)
  418. return string.Empty;
  419. if (this.NodeType != XmlNodeType.Element)
  420. MoveToElement();
  421. if (this.NodeType == XmlNodeType.Element)
  422. {
  423. if (this.IsEmptyElement)
  424. return string.Empty;
  425. if (!Read())
  426. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidOperation)));
  427. if (this.NodeType == XmlNodeType.EndElement)
  428. return string.Empty;
  429. }
  430. StringBuilder sb = null;
  431. string result = string.Empty;
  432. while (IsTextNode(this.NodeType))
  433. {
  434. string value = this.Value;
  435. if (result.Length == 0)
  436. {
  437. result = value;
  438. }
  439. else
  440. {
  441. if (sb == null)
  442. sb = new StringBuilder(result);
  443. if (sb.Length > maxStringContentLength - value.Length)
  444. XmlExceptionHelper.ThrowMaxStringContentLengthExceeded(this, maxStringContentLength);
  445. sb.Append(value);
  446. }
  447. if (!Read())
  448. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlInvalidOperation)));
  449. }
  450. if (sb != null)
  451. result = sb.ToString();
  452. if (result.Length > maxStringContentLength)
  453. XmlExceptionHelper.ThrowMaxStringContentLengthExceeded(this, maxStringContentLength);
  454. return result;
  455. }
  456. public virtual byte[] ReadContentAsBinHex()
  457. {
  458. return ReadContentAsBinHex(Quotas.MaxArrayLength);
  459. }
  460. protected byte[] ReadContentAsBinHex(int maxByteArrayContentLength)
  461. {
  462. return ReadContentAsBytes(false, maxByteArrayContentLength);
  463. }
  464. byte[] ReadContentAsBytes(bool base64, int maxByteArrayContentLength)
  465. {
  466. byte[][] buffers = new byte[32][];
  467. byte[] buffer;
  468. // Its best to read in buffers that are a multiple of 3 so we don't break base64 boundaries when converting text
  469. int count = 384;
  470. int bufferCount = 0;
  471. int totalRead = 0;
  472. while (true)
  473. {
  474. buffer = new byte[count];
  475. buffers[bufferCount++] = buffer;
  476. int read = 0;
  477. while (read < buffer.Length)
  478. {
  479. int actual;
  480. if (base64)
  481. actual = ReadContentAsBase64(buffer, read, buffer.Length - read);
  482. else
  483. actual = ReadContentAsBinHex(buffer, read, buffer.Length - read);
  484. if (actual == 0)
  485. break;
  486. read += actual;
  487. }
  488. if (totalRead > maxByteArrayContentLength - read)
  489. XmlExceptionHelper.ThrowMaxArrayLengthExceeded(this, maxByteArrayContentLength);
  490. totalRead += read;
  491. if (read < buffer.Length)
  492. break;
  493. count = count * 2;
  494. }
  495. buffer = new byte[totalRead];
  496. int offset = 0;
  497. for (int i = 0; i < bufferCount - 1; i++)
  498. {
  499. Buffer.BlockCopy(buffers[i], 0, buffer, offset, buffers[i].Length);
  500. offset += buffers[i].Length;
  501. }
  502. Buffer.BlockCopy(buffers[bufferCount - 1], 0, buffer, offset, totalRead - offset);
  503. return buffer;
  504. }
  505. protected bool IsTextNode(XmlNodeType nodeType)
  506. {
  507. return nodeType == XmlNodeType.Text ||
  508. nodeType == XmlNodeType.Whitespace ||
  509. nodeType == XmlNodeType.SignificantWhitespace ||
  510. nodeType == XmlNodeType.CDATA ||
  511. nodeType == XmlNodeType.Attribute;
  512. }
  513. public virtual int ReadContentAsChars(char[] chars, int offset, int count)
  514. {
  515. int read = 0;
  516. while (true)
  517. {
  518. XmlNodeType nodeType = this.NodeType;
  519. if (nodeType == XmlNodeType.Element || nodeType == XmlNodeType.EndElement)
  520. break;
  521. if (IsTextNode(nodeType))
  522. {
  523. read = ReadValueChunk(chars, offset, count);
  524. if (read > 0)
  525. break;
  526. if (nodeType == XmlNodeType.Attribute /* || inAttributeText */)
  527. break;
  528. if (!Read())
  529. break;
  530. }
  531. else
  532. {
  533. if (!Read())
  534. break;
  535. }
  536. }
  537. return read;
  538. }
  539. public override object ReadContentAs(Type type, IXmlNamespaceResolver namespaceResolver)
  540. {
  541. if (type == typeof(Guid[]))
  542. {
  543. string[] values = (string[])ReadContentAs(typeof(string[]), namespaceResolver);
  544. Guid[] guids = new Guid[values.Length];
  545. for (int i = 0; i < values.Length; i++)
  546. guids[i] = XmlConverter.ToGuid(values[i]);
  547. return guids;
  548. }
  549. if (type == typeof(UniqueId[]))
  550. {
  551. string[] values = (string[])ReadContentAs(typeof(string[]), namespaceResolver);
  552. UniqueId[] uniqueIds = new UniqueId[values.Length];
  553. for (int i = 0; i < values.Length; i++)
  554. uniqueIds[i] = XmlConverter.ToUniqueId(values[i]);
  555. return uniqueIds;
  556. }
  557. return base.ReadContentAs(type, namespaceResolver);
  558. }
  559. public virtual string ReadContentAsString(string[] strings, out int index)
  560. {
  561. if (strings == null)
  562. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("strings");
  563. string s = ReadContentAsString();
  564. index = -1;
  565. for (int i = 0; i < strings.Length; i++)
  566. {
  567. string value = strings[i];
  568. if (value == null)
  569. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(string.Format(CultureInfo.InvariantCulture, "strings[{0}]", i));
  570. if (value == s)
  571. {
  572. index = i;
  573. return value;
  574. }
  575. }
  576. return s;
  577. }
  578. public virtual string ReadContentAsString(XmlDictionaryString[] strings, out int index)
  579. {
  580. if (strings == null)
  581. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("strings");
  582. string s = ReadContentAsString();
  583. index = -1;
  584. for (int i = 0; i < strings.Length; i++)
  585. {
  586. XmlDictionaryString value = strings[i];
  587. if (value == null)
  588. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(string.Format(CultureInfo.InvariantCulture, "strings[{0}]", i));
  589. if (value.Value == s)
  590. {
  591. index = i;
  592. return value.Value;
  593. }
  594. }
  595. return s;
  596. }
  597. public override decimal ReadContentAsDecimal()
  598. {
  599. return XmlConverter.ToDecimal(ReadContentAsString());
  600. }
  601. public override Single ReadContentAsFloat()
  602. {
  603. return XmlConverter.ToSingle(ReadContentAsString());
  604. }
  605. public virtual UniqueId ReadContentAsUniqueId()
  606. {
  607. return XmlConverter.ToUniqueId(ReadContentAsString());
  608. }
  609. public virtual Guid ReadContentAsGuid()
  610. {
  611. return XmlConverter.ToGuid(ReadContentAsString());
  612. }
  613. public virtual TimeSpan ReadContentAsTimeSpan()
  614. {
  615. return XmlConverter.ToTimeSpan(ReadContentAsString());
  616. }
  617. public virtual void ReadContentAsQualifiedName(out string localName, out string namespaceUri)
  618. {
  619. string prefix;
  620. XmlConverter.ToQualifiedName(ReadContentAsString(), out prefix, out localName);
  621. namespaceUri = LookupNamespace(prefix);
  622. if (namespaceUri == null)
  623. XmlExceptionHelper.ThrowUndefinedPrefix(this, prefix);
  624. }
  625. /* string, bool, int, long, float, double, decimal, DateTime, base64, binhex, uniqueID, object, list*/
  626. public override string ReadElementContentAsString()
  627. {
  628. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  629. string value;
  630. if (isEmptyElement)
  631. {
  632. Read();
  633. value = string.Empty;
  634. }
  635. else
  636. {
  637. ReadStartElement();
  638. value = ReadContentAsString();
  639. ReadEndElement();
  640. }
  641. return value;
  642. }
  643. public override bool ReadElementContentAsBoolean()
  644. {
  645. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  646. bool value;
  647. if (isEmptyElement)
  648. {
  649. Read();
  650. value = XmlConverter.ToBoolean(string.Empty);
  651. }
  652. else
  653. {
  654. ReadStartElement();
  655. value = ReadContentAsBoolean();
  656. ReadEndElement();
  657. }
  658. return value;
  659. }
  660. public override int ReadElementContentAsInt()
  661. {
  662. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  663. int value;
  664. if (isEmptyElement)
  665. {
  666. Read();
  667. value = XmlConverter.ToInt32(string.Empty);
  668. }
  669. else
  670. {
  671. ReadStartElement();
  672. value = ReadContentAsInt();
  673. ReadEndElement();
  674. }
  675. return value;
  676. }
  677. public override long ReadElementContentAsLong()
  678. {
  679. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  680. long value;
  681. if (isEmptyElement)
  682. {
  683. Read();
  684. value = XmlConverter.ToInt64(string.Empty);
  685. }
  686. else
  687. {
  688. ReadStartElement();
  689. value = ReadContentAsLong();
  690. ReadEndElement();
  691. }
  692. return value;
  693. }
  694. public override float ReadElementContentAsFloat()
  695. {
  696. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  697. float value;
  698. if (isEmptyElement)
  699. {
  700. Read();
  701. value = XmlConverter.ToSingle(string.Empty);
  702. }
  703. else
  704. {
  705. ReadStartElement();
  706. value = ReadContentAsFloat();
  707. ReadEndElement();
  708. }
  709. return value;
  710. }
  711. public override double ReadElementContentAsDouble()
  712. {
  713. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  714. double value;
  715. if (isEmptyElement)
  716. {
  717. Read();
  718. value = XmlConverter.ToDouble(string.Empty);
  719. }
  720. else
  721. {
  722. ReadStartElement();
  723. value = ReadContentAsDouble();
  724. ReadEndElement();
  725. }
  726. return value;
  727. }
  728. public override decimal ReadElementContentAsDecimal()
  729. {
  730. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  731. decimal value;
  732. if (isEmptyElement)
  733. {
  734. Read();
  735. value = XmlConverter.ToDecimal(string.Empty);
  736. }
  737. else
  738. {
  739. ReadStartElement();
  740. value = ReadContentAsDecimal();
  741. ReadEndElement();
  742. }
  743. return value;
  744. }
  745. public override DateTime ReadElementContentAsDateTime()
  746. {
  747. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  748. DateTime value;
  749. if (isEmptyElement)
  750. {
  751. Read();
  752. try
  753. {
  754. value = DateTime.Parse(string.Empty, NumberFormatInfo.InvariantInfo);
  755. }
  756. catch (ArgumentException exception)
  757. {
  758. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "DateTime", exception));
  759. }
  760. catch (FormatException exception)
  761. {
  762. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "DateTime", exception));
  763. }
  764. }
  765. else
  766. {
  767. ReadStartElement();
  768. value = ReadContentAsDateTime();
  769. ReadEndElement();
  770. }
  771. return value;
  772. }
  773. public virtual UniqueId ReadElementContentAsUniqueId()
  774. {
  775. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  776. UniqueId value;
  777. if (isEmptyElement)
  778. {
  779. Read();
  780. try
  781. {
  782. value = new UniqueId(string.Empty);
  783. }
  784. catch (ArgumentException exception)
  785. {
  786. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "UniqueId", exception));
  787. }
  788. catch (FormatException exception)
  789. {
  790. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "UniqueId", exception));
  791. }
  792. }
  793. else
  794. {
  795. ReadStartElement();
  796. value = ReadContentAsUniqueId();
  797. ReadEndElement();
  798. }
  799. return value;
  800. }
  801. [SuppressMessage("Reliability", "Reliability113", Justification = "Catching expected exceptions inline instead of calling Fx.CreateGuid to minimize code change")]
  802. public virtual Guid ReadElementContentAsGuid()
  803. {
  804. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  805. Guid value;
  806. if (isEmptyElement)
  807. {
  808. Read();
  809. try
  810. {
  811. value = Guid.Empty;
  812. }
  813. catch (ArgumentException exception)
  814. {
  815. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "Guid", exception));
  816. }
  817. catch (FormatException exception)
  818. {
  819. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "Guid", exception));
  820. }
  821. catch (OverflowException exception)
  822. {
  823. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlExceptionHelper.CreateConversionException(string.Empty, "Guid", exception));
  824. }
  825. }
  826. else
  827. {
  828. ReadStartElement();
  829. value = ReadContentAsGuid();
  830. ReadEndElement();
  831. }
  832. return value;
  833. }
  834. public virtual TimeSpan ReadElementContentAsTimeSpan()
  835. {
  836. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  837. TimeSpan value;
  838. if (isEmptyElement)
  839. {
  840. Read();
  841. value = XmlConverter.ToTimeSpan(string.Empty);
  842. }
  843. else
  844. {
  845. ReadStartElement();
  846. value = ReadContentAsTimeSpan();
  847. ReadEndElement();
  848. }
  849. return value;
  850. }
  851. public virtual byte[] ReadElementContentAsBase64()
  852. {
  853. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  854. byte[] buffer;
  855. if (isEmptyElement)
  856. {
  857. Read();
  858. buffer = new byte[0];
  859. }
  860. else
  861. {
  862. ReadStartElement();
  863. buffer = ReadContentAsBase64();
  864. ReadEndElement();
  865. }
  866. return buffer;
  867. }
  868. public virtual byte[] ReadElementContentAsBinHex()
  869. {
  870. bool isEmptyElement = IsStartElement() && IsEmptyElement;
  871. byte[] buffer;
  872. if (isEmptyElement)
  873. {
  874. Read();
  875. buffer = new byte[0];
  876. }
  877. else
  878. {
  879. ReadStartElement();
  880. buffer = ReadContentAsBinHex();
  881. ReadEndElement();
  882. }
  883. return buffer;
  884. }
  885. public virtual void GetNonAtomizedNames(out string localName, out string namespaceUri)
  886. {
  887. localName = this.LocalName;
  888. namespaceUri = this.NamespaceURI;
  889. }
  890. public virtual bool TryGetLocalNameAsDictionaryString(out XmlDictionaryString localName)
  891. {
  892. localName = null;
  893. return false;
  894. }
  895. public virtual bool TryGetNamespaceUriAsDictionaryString(out XmlDictionaryString namespaceUri)
  896. {
  897. namespaceUri = null;
  898. return false;
  899. }
  900. public virtual bool TryGetValueAsDictionaryString(out XmlDictionaryString value)
  901. {
  902. value = null;
  903. return false;
  904. }
  905. void CheckArray(Array array, int offset, int count)
  906. {
  907. if (array == null)
  908. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("array"));
  909. if (offset < 0)
  910. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.ValueMustBeNonNegative)));
  911. if (offset > array.Length)
  912. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("offset", SR.GetString(SR.OffsetExceedsBufferSize, array.Length)));
  913. if (count < 0)
  914. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.ValueMustBeNonNegative)));
  915. if (count > array.Length - offset)
  916. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("count", SR.GetString(SR.SizeExceedsRemainingBufferSpace, array.Length - offset)));
  917. }
  918. public virtual bool IsStartArray(out Type type)
  919. {
  920. type = null;
  921. return false;
  922. }
  923. public virtual bool TryGetArrayLength(out int count)
  924. {
  925. count = 0;
  926. return false;
  927. }
  928. // Boolean
  929. public virtual bool[] ReadBooleanArray(string localName, string namespaceUri)
  930. {
  931. return BooleanArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  932. }
  933. public virtual bool[] ReadBooleanArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  934. {
  935. return BooleanArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  936. }
  937. public virtual int ReadArray(string localName, string namespaceUri, bool[] array, int offset, int count)
  938. {
  939. CheckArray(array, offset, count);
  940. int actual = 0;
  941. while (actual < count && IsStartElement(localName, namespaceUri))
  942. {
  943. array[offset + actual] = ReadElementContentAsBoolean();
  944. actual++;
  945. }
  946. return actual;
  947. }
  948. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, bool[] array, int offset, int count)
  949. {
  950. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  951. }
  952. // Int16
  953. public virtual Int16[] ReadInt16Array(string localName, string namespaceUri)
  954. {
  955. return Int16ArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  956. }
  957. public virtual Int16[] ReadInt16Array(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  958. {
  959. return Int16ArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  960. }
  961. public virtual int ReadArray(string localName, string namespaceUri, Int16[] array, int offset, int count)
  962. {
  963. CheckArray(array, offset, count);
  964. int actual = 0;
  965. while (actual < count && IsStartElement(localName, namespaceUri))
  966. {
  967. int i = ReadElementContentAsInt();
  968. if (i < Int16.MinValue || i > Int16.MaxValue)
  969. XmlExceptionHelper.ThrowConversionOverflow(this, i.ToString(NumberFormatInfo.CurrentInfo), "Int16");
  970. array[offset + actual] = (Int16)i;
  971. actual++;
  972. }
  973. return actual;
  974. }
  975. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int16[] array, int offset, int count)
  976. {
  977. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  978. }
  979. // Int32
  980. public virtual Int32[] ReadInt32Array(string localName, string namespaceUri)
  981. {
  982. return Int32ArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  983. }
  984. public virtual Int32[] ReadInt32Array(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  985. {
  986. return Int32ArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  987. }
  988. public virtual int ReadArray(string localName, string namespaceUri, Int32[] array, int offset, int count)
  989. {
  990. CheckArray(array, offset, count);
  991. int actual = 0;
  992. while (actual < count && IsStartElement(localName, namespaceUri))
  993. {
  994. array[offset + actual] = ReadElementContentAsInt();
  995. actual++;
  996. }
  997. return actual;
  998. }
  999. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int32[] array, int offset, int count)
  1000. {
  1001. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1002. }
  1003. // Int64
  1004. public virtual Int64[] ReadInt64Array(string localName, string namespaceUri)
  1005. {
  1006. return Int64ArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1007. }
  1008. public virtual Int64[] ReadInt64Array(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1009. {
  1010. return Int64ArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1011. }
  1012. public virtual int ReadArray(string localName, string namespaceUri, Int64[] array, int offset, int count)
  1013. {
  1014. CheckArray(array, offset, count);
  1015. int actual = 0;
  1016. while (actual < count && IsStartElement(localName, namespaceUri))
  1017. {
  1018. array[offset + actual] = ReadElementContentAsLong();
  1019. actual++;
  1020. }
  1021. return actual;
  1022. }
  1023. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, Int64[] array, int offset, int count)
  1024. {
  1025. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1026. }
  1027. // Single
  1028. public virtual float[] ReadSingleArray(string localName, string namespaceUri)
  1029. {
  1030. return SingleArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1031. }
  1032. public virtual float[] ReadSingleArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1033. {
  1034. return SingleArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1035. }
  1036. public virtual int ReadArray(string localName, string namespaceUri, float[] array, int offset, int count)
  1037. {
  1038. CheckArray(array, offset, count);
  1039. int actual = 0;
  1040. while (actual < count && IsStartElement(localName, namespaceUri))
  1041. {
  1042. array[offset + actual] = ReadElementContentAsFloat();
  1043. actual++;
  1044. }
  1045. return actual;
  1046. }
  1047. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, float[] array, int offset, int count)
  1048. {
  1049. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1050. }
  1051. // Double
  1052. public virtual double[] ReadDoubleArray(string localName, string namespaceUri)
  1053. {
  1054. return DoubleArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1055. }
  1056. public virtual double[] ReadDoubleArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1057. {
  1058. return DoubleArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1059. }
  1060. public virtual int ReadArray(string localName, string namespaceUri, double[] array, int offset, int count)
  1061. {
  1062. CheckArray(array, offset, count);
  1063. int actual = 0;
  1064. while (actual < count && IsStartElement(localName, namespaceUri))
  1065. {
  1066. array[offset + actual] = ReadElementContentAsDouble();
  1067. actual++;
  1068. }
  1069. return actual;
  1070. }
  1071. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, double[] array, int offset, int count)
  1072. {
  1073. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1074. }
  1075. // Decimal
  1076. public virtual decimal[] ReadDecimalArray(string localName, string namespaceUri)
  1077. {
  1078. return DecimalArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1079. }
  1080. public virtual decimal[] ReadDecimalArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1081. {
  1082. return DecimalArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1083. }
  1084. public virtual int ReadArray(string localName, string namespaceUri, decimal[] array, int offset, int count)
  1085. {
  1086. CheckArray(array, offset, count);
  1087. int actual = 0;
  1088. while (actual < count && IsStartElement(localName, namespaceUri))
  1089. {
  1090. array[offset + actual] = ReadElementContentAsDecimal();
  1091. actual++;
  1092. }
  1093. return actual;
  1094. }
  1095. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, decimal[] array, int offset, int count)
  1096. {
  1097. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1098. }
  1099. // DateTime
  1100. public virtual DateTime[] ReadDateTimeArray(string localName, string namespaceUri)
  1101. {
  1102. return DateTimeArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1103. }
  1104. public virtual DateTime[] ReadDateTimeArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1105. {
  1106. return DateTimeArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1107. }
  1108. public virtual int ReadArray(string localName, string namespaceUri, DateTime[] array, int offset, int count)
  1109. {
  1110. CheckArray(array, offset, count);
  1111. int actual = 0;
  1112. while (actual < count && IsStartElement(localName, namespaceUri))
  1113. {
  1114. array[offset + actual] = ReadElementContentAsDateTime();
  1115. actual++;
  1116. }
  1117. return actual;
  1118. }
  1119. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, DateTime[] array, int offset, int count)
  1120. {
  1121. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1122. }
  1123. // Guid
  1124. public virtual Guid[] ReadGuidArray(string localName, string namespaceUri)
  1125. {
  1126. return GuidArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1127. }
  1128. public virtual Guid[] ReadGuidArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1129. {
  1130. return GuidArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1131. }
  1132. public virtual int ReadArray(string localName, string namespaceUri, Guid[] array, int offset, int count)
  1133. {
  1134. CheckArray(array, offset, count);
  1135. int actual = 0;
  1136. while (actual < count && IsStartElement(localName, namespaceUri))
  1137. {
  1138. array[offset + actual] = ReadElementContentAsGuid();
  1139. actual++;
  1140. }
  1141. return actual;
  1142. }
  1143. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, Guid[] array, int offset, int count)
  1144. {
  1145. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1146. }
  1147. // TimeSpan
  1148. public virtual TimeSpan[] ReadTimeSpanArray(string localName, string namespaceUri)
  1149. {
  1150. return TimeSpanArrayHelperWithString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1151. }
  1152. public virtual TimeSpan[] ReadTimeSpanArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
  1153. {
  1154. return TimeSpanArrayHelperWithDictionaryString.Instance.ReadArray(this, localName, namespaceUri, Quotas.MaxArrayLength);
  1155. }
  1156. public virtual int ReadArray(string localName, string namespaceUri, TimeSpan[] array, int offset, int count)
  1157. {
  1158. CheckArray(array, offset, count);
  1159. int actual = 0;
  1160. while (actual < count && IsStartElement(localName, namespaceUri))
  1161. {
  1162. array[offset + actual] = ReadElementContentAsTimeSpan();
  1163. actual++;
  1164. }
  1165. return actual;
  1166. }
  1167. public virtual int ReadArray(XmlDictionaryString localName, XmlDictionaryString namespaceUri, TimeSpan[] array, int offset, int count)
  1168. {
  1169. return ReadArray(XmlDictionaryString.GetString(localName), XmlDictionaryString.GetString(namespaceUri), array, offset, count);
  1170. }
  1171. class XmlWrappedReader : XmlDictionaryReader, IXmlLineInfo
  1172. {
  1173. XmlReader reader;
  1174. XmlNamespaceManager nsMgr;
  1175. public XmlWrappedReader(XmlReader reader, XmlNamespaceManager nsMgr)
  1176. {
  1177. this.reader = reader;
  1178. this.nsMgr = nsMgr;
  1179. }
  1180. public override int AttributeCount
  1181. {
  1182. get
  1183. {
  1184. return reader.AttributeCount;
  1185. }
  1186. }
  1187. public override string BaseURI
  1188. {
  1189. get
  1190. {
  1191. return reader.BaseURI;
  1192. }
  1193. }
  1194. public override bool CanReadBinaryContent
  1195. {
  1196. get { return reader.CanReadBinaryContent; }
  1197. }
  1198. public override bool CanReadValueChunk
  1199. {
  1200. get { return reader.CanReadValueChunk; }
  1201. }
  1202. public override void Close()
  1203. {
  1204. reader.Close();
  1205. nsMgr = null;
  1206. }
  1207. public override int Depth
  1208. {
  1209. get
  1210. {
  1211. return reader.Depth;
  1212. }
  1213. }
  1214. public override bool EOF
  1215. {
  1216. get
  1217. {
  1218. return reader.EOF;
  1219. }
  1220. }
  1221. public override string GetAttribute(int index)
  1222. {
  1223. return reader.GetAttribute(index);
  1224. }
  1225. public override string GetAttribute(string name)
  1226. {
  1227. return reader.GetAttribute(name);
  1228. }
  1229. public override string GetAttribute(string name, string namespaceUri)
  1230. {
  1231. return reader.GetAttribute(name, namespaceUri);
  1232. }
  1233. public override bool HasValue
  1234. {
  1235. get
  1236. {
  1237. return reader.HasValue;
  1238. }
  1239. }
  1240. public override bool IsDefault
  1241. {
  1242. get
  1243. {
  1244. return reader.IsDefault;
  1245. }
  1246. }
  1247. public override bool IsEmptyElement
  1248. {
  1249. get
  1250. {
  1251. return reader.IsEmptyElement;
  1252. }
  1253. }
  1254. public override bool IsStartElement(string name)
  1255. {
  1256. return reader.IsStartElement(name);
  1257. }
  1258. public override bool IsStartElement(string localName, string namespaceUri)
  1259. {
  1260. return reader.IsStartElement(localName, namespaceUri);
  1261. }
  1262. public override string LocalName
  1263. {
  1264. get
  1265. {
  1266. return reader.LocalName;
  1267. }
  1268. }
  1269. public override string LookupNamespace(string namespaceUri)
  1270. {
  1271. return reader.LookupNamespace(namespaceUri);
  1272. }
  1273. public override void MoveToAttribute(int index)
  1274. {
  1275. reader.MoveToAttribute(index);
  1276. }
  1277. public override bool MoveToAttribute(string name)
  1278. {
  1279. return reader.MoveToAttribute(name);
  1280. }
  1281. public override bool MoveToAttribute(string name, string namespaceUri)
  1282. {
  1283. return reader.MoveToAttribute(name, namespaceUri);
  1284. }
  1285. public override bool MoveToElement()
  1286. {
  1287. return reader.MoveToElement();
  1288. }
  1289. public override bool MoveToFirstAttribute()
  1290. {
  1291. return reader.MoveToFirstAttribute();
  1292. }
  1293. public override bool MoveToNextAttribute()
  1294. {
  1295. return reader.MoveToNextAttribute();
  1296. }
  1297. public override string Name
  1298. {
  1299. get
  1300. {
  1301. return reader.Name;
  1302. }
  1303. }
  1304. public override string NamespaceURI
  1305. {
  1306. get
  1307. {
  1308. return reader.NamespaceURI;
  1309. }
  1310. }
  1311. public override XmlNameTable NameTable
  1312. {
  1313. get
  1314. {
  1315. return reader.NameTable;
  1316. }
  1317. }
  1318. public override XmlNodeType NodeType
  1319. {
  1320. get
  1321. {
  1322. return reader.NodeType;
  1323. }
  1324. }
  1325. public override string Prefix
  1326. {
  1327. get
  1328. {
  1329. return reader.Prefix;
  1330. }
  1331. }
  1332. public override char QuoteChar
  1333. {
  1334. get
  1335. {
  1336. return reader.QuoteChar;
  1337. }
  1338. }
  1339. public override bool Read()
  1340. {
  1341. return reader.Read();
  1342. }
  1343. public override bool ReadAttributeValue()
  1344. {
  1345. return reader.ReadAttributeValue();
  1346. }
  1347. public override string ReadElementString(string name)
  1348. {
  1349. return reader.ReadElementString(name);
  1350. }
  1351. public override string ReadElementString(string localName, string namespaceUri)
  1352. {
  1353. return reader.ReadElementString(localName, namespaceUri);
  1354. }
  1355. public override string ReadInnerXml()
  1356. {
  1357. return reader.ReadInnerXml();
  1358. }
  1359. public override string ReadOuterXml()
  1360. {
  1361. return reader.ReadOuterXml();
  1362. }
  1363. public override void ReadStartElement(string name)
  1364. {
  1365. reader.ReadStartElement(name);
  1366. }
  1367. public override void ReadStartElement(string localName, string namespaceUri)
  1368. {
  1369. reader.ReadStartElement(localName, namespaceUri);
  1370. }
  1371. public override void ReadEndElement()
  1372. {
  1373. reader.ReadEndElement();
  1374. }
  1375. public override string ReadString()
  1376. {
  1377. return reader.ReadString();
  1378. }
  1379. public override ReadState ReadState
  1380. {
  1381. get
  1382. {
  1383. return reader.ReadState;
  1384. }
  1385. }
  1386. public override void ResolveEntity()
  1387. {
  1388. reader.ResolveEntity();
  1389. }
  1390. public override string this[int index]
  1391. {
  1392. get
  1393. {
  1394. return reader[index];
  1395. }
  1396. }
  1397. public override string this[string name]
  1398. {
  1399. get
  1400. {
  1401. return reader[name];
  1402. }
  1403. }
  1404. public override string this[string name, string namespaceUri]
  1405. {
  1406. get
  1407. {
  1408. return reader[name, namespaceUri];
  1409. }
  1410. }
  1411. public override string Value
  1412. {
  1413. get
  1414. {
  1415. return reader.Value;
  1416. }
  1417. }
  1418. public override string XmlLang
  1419. {
  1420. get
  1421. {
  1422. return reader.XmlLang;
  1423. }
  1424. }
  1425. public override XmlSpace XmlSpace
  1426. {
  1427. get
  1428. {
  1429. return reader.XmlSpace;
  1430. }
  1431. }
  1432. public override int ReadElementContentAsBase64(byte[] buffer, int offset, int count)
  1433. {
  1434. return reader.ReadElementContentAsBase64(buffer, offset, count);
  1435. }
  1436. public override int ReadContentAsBase64(byte[] buffer, int offset, int count)
  1437. {
  1438. return reader.ReadContentAsBase64(buffer, offset, count);
  1439. }
  1440. public override int ReadElementContentAsBinHex(byte[] buffer, int offset, int count)
  1441. {
  1442. return reader.ReadElementContentAsBinHex(buffer, offset, count);
  1443. }
  1444. public override int ReadContentAsBinHex(byte[] buffer, int offset, int count)
  1445. {
  1446. return reader.ReadContentAsBinHex(buffer, offset, count);
  1447. }
  1448. public override int ReadValueChunk(char[] chars, int offset, int count)
  1449. {
  1450. return reader.ReadValueChunk(chars, offset, count);
  1451. }
  1452. public override Type ValueType
  1453. {
  1454. get
  1455. {
  1456. return reader.ValueType;
  1457. }
  1458. }
  1459. public override Boolean ReadContentAsBoolean()
  1460. {
  1461. return reader.ReadContentAsBoolean();
  1462. }
  1463. public override DateTime ReadContentAsDateTime()
  1464. {
  1465. return reader.ReadContentAsDateTime();
  1466. }
  1467. public override Decimal ReadContentAsDecimal()
  1468. {
  1469. // return reader.ReadContentAsDecimal();
  1470. return (Decimal)reader.ReadContentAs(typeof(Decimal), null);
  1471. }
  1472. public override Double ReadContentAsDouble()
  1473. {
  1474. return reader.ReadContentAsDouble();
  1475. }
  1476. public override Int32 ReadContentAsInt()
  1477. {
  1478. return reader.ReadContentAsInt();
  1479. }
  1480. public override Int64 ReadContentAsLong()
  1481. {
  1482. return reader.ReadContentAsLong();
  1483. }
  1484. public override Single ReadContentAsFloat()
  1485. {
  1486. return reader.ReadContentAsFloat();
  1487. }
  1488. public override string ReadContentAsString()
  1489. {
  1490. return reader.ReadContentAsString();
  1491. }
  1492. public override object ReadContentAs(Type type, IXmlNamespaceResolver namespaceResolver)
  1493. {
  1494. return reader.ReadContentAs(type, namespaceResolver);
  1495. }
  1496. public bool HasLineInfo()
  1497. {
  1498. IXmlLineInfo lineInfo = reader as IXmlLineInfo;
  1499. if (lineInfo == null)
  1500. return false;
  1501. return lineInfo.HasLineInfo();
  1502. }
  1503. public int LineNumber
  1504. {
  1505. get
  1506. {
  1507. IXmlLineInfo lineInfo = reader as IXmlLineInfo;
  1508. if (lineInfo == null)
  1509. return 1;
  1510. return lineInfo.LineNumber;
  1511. }
  1512. }
  1513. public int LinePosition
  1514. {
  1515. get
  1516. {
  1517. IXmlLineInfo lineInfo = reader as IXmlLineInfo;
  1518. if (lineInfo == null)
  1519. return 1;
  1520. return lineInfo.LinePosition;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. }