2 // System.Data.SqlClient.SqlXmlTextReader.cs
5 // Konstantin Triger (kostat@mainsoft.com)
7 // Copyright (C) 2006 Mainsoft, corp. (http://www.mainsoft.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 namespace System.Data.SqlClient {
35 internal sealed class SqlXmlTextReader : TextReader {
37 #region FragmentXmlTextReader
39 sealed class FragmentXmlTextReader : XmlTextReader {
40 public FragmentXmlTextReader(System.IO.TextReader reader) : base(reader) {}
42 public override bool Read() {
46 }while(base.Depth == 0);
51 public override int Depth {
53 int depth = base.Depth;
67 readonly char[] _peekedChar = new char[1];
68 readonly SqlDataReader _reader;
75 static readonly char[] OpenRoot = new char[] {'<', 'X', '>'};
76 const int OpenRootLength = 3;
77 static readonly char[] CloseRoot = new char[] {'<', '/', 'X', '>'};
78 const int CloseRootLength = 4;
84 private SqlXmlTextReader (SqlDataReader reader) {
92 public static XmlReader Create(SqlDataReader dataReader) {
93 return new FragmentXmlTextReader(new SqlXmlTextReader(dataReader));
96 public override void Close() {
100 public override int Peek () {
101 if (!_hasPeekedChar) {
103 int consumed = Read(_peekedChar, 0, 1);
107 _hasPeekedChar = true;
110 return _peekedChar[0];
113 public override int Read () {
115 _hasPeekedChar = false;
119 public override int Read (char[] buffer, int index, int count) {
121 throw new ArgumentNullException("buffer");
124 throw new ArgumentOutOfRangeException("index");
127 throw new ArgumentOutOfRangeException("count");
134 if (_hasPeekedChar) {
135 buffer[index++] = _peekedChar[0];
137 _hasPeekedChar = false;
144 if (_rootPosition < OpenRootLength) {
145 buffer[index++] = OpenRoot[_rootPosition++];
152 if (_reader.Read()) {
154 _data = _reader.GetString(0);
157 if(_reader.NextResult())
167 int consumed = ((_position + count) > _data.Length) ? (_data.Length - (int)_position) : count;
168 _data.CopyTo(_position, buffer, index, consumed);
171 _position += consumed;
181 while (count > 0 && _rootPosition < CloseRootLength) {
182 buffer[index++] = CloseRoot[_rootPosition++];
191 #endregion // Methods